0

私はおそらく私に代わって小さな見落としに直面していますが、ビューで複雑なクラスをシリアル化しようとしています。このコードを実行すると、循環参照を取得し続けます。

コントローラ:

public JsonResult EditPrice(int id)
{
    Category cat = _categoryDataGateway.GetCategory(id);
    return Json(new {category = cat}, JsonRequestBehavior.AllowGet);
}

Jquery:

<script type="text/javascript">
function PriceChange(e) {
    if (e.name == "PriceCategory") {
        var priceWindow = $("#PriceWindow").data("tWindow");
        var category = e.response.category;
        $("#PriceDetails")
            .find("h2")
            .text(category.Name);
        priceWindow.center().open();
    }
}
</script>

ビュー+テレリック:

<div>
    @(Html.Telerik().Grid(Model)
    .Name("CategoryList")
    .Localizable("is-IS")
    .Columns(columns =>
    {
        columns.Bound(o => o.Name).Width("20%");
        columns.Bound(o => o.Id).Template(o => Html.Label(o.SuperCategory())).Title("Yfirflokkur").Width("15%");
        columns.Bound(o => o.Description).Width("35%");
        columns.Command(command =>
        {
            command.Custom("EditCategory").Text("Edit").Action("Edit", "Category").DataRouteValues(r => r.Add(k => k.Id));
            command.Custom("PriceCategory").Text("Price").Action("EditPrice", "Category").DataRouteValues(r => r.Add(k => k.Id).RouteKey("Id")).Ajax(true);
            command.Custom("DeleteCategory").Text("Delete").Action("Delete", "Category").DataRouteValues(r => r.Add(k => k.Id));
        }).Width("30%").Title("Actions");
    })
    .ClientEvents(events => events.OnComplete("PriceChange"))
    .Sortable()
    .Footer(false)
)
</div>
<div>
    @(Html.Telerik().Window()
        .Name("PriceWindow")
        .Visible(false)
        .Title("Price")
        .Modal(true)
        .Width(500)
        .Height(200)
        .Content(@<text>
                    <div id="PriceDetails">
                        <h2></h2>
                        <div>
                            <input id="CategoryPrice" type="text"/>
                        </div>
                        <div>
                            <button>Submit</button>
                        </div>
                    </div>
            </text>)
    )
</div>

私の目標は、グリッドの[価格]ボタンを押すことができるようにすることです。ウィンドウがポップアップして、価格を変更できます。問題は、私はすでにこのコードを使用して単純な文字列をビューに送信することができ、それは完全に機能することです。たとえば、「cat」だけでなく「cat.Name」をコントローラーから送信するので、基本的に送信するとき複合型「Category」循環参照を取得します。
any1は私の失敗を見つけることができますか?:)

4

1 に答える 1

0

私は自分の質問に答えると信じています。質問を投稿した直後に、他のことを試しましたが、次のように機能しました。

コントローラ:

public JsonResult EditPrice(int id)
{
    Category cat = _categoryDataGateway.GetCategory(id);
    return Json(new {Id = cat.Id, Name = cat.Name}, JsonRequestBehavior.AllowGet);
}

Jquery:

<script type="text/javascript">
function PriceChange(e) {
    if (e.name == "PriceCategory") {
        var priceWindow = $("#PriceWindow").data("tWindow");
        var id = e.response.Id;
        var name = e.response.Name;
        $("#PriceDetails")
            .find("h2")
            .text(name)
            .end()
            .find("#CategoryIdHidden")
            .attr("value", id);
        priceWindow.center().open();
    }
}
</script>

Telerikウィンドウ:

<div>
    @(Html.Telerik().Window()
        .Name("PriceWindow")
        .Visible(false)
        .Title("Verð")
        .Modal(true)
        .Width(300)
        .Height(200)
        .Content(@<text>
                    <div id="PriceDetails">
                        <h2></h2>
                        <div>
                            <input id="CategoryPrice" type="text"/>
                            <input id="CategoryIdHidden" type="hidden"/>
                        </div>
                        <div>
                            <span class="art-button-wrapper"onmouseover="className='art-button-wrapper hover'"
                                    onmouseout="className='art-button-wrapper'">
                                <span class="art-button-l"></span><span class="art-button-r "></span>
                                <button class="art-button">Breyta verði</button>
                            </span>
                        </div>
                    </div>
            </text>)
    )
</div>

重要なのは、複合型からビットを取得して、そこでシリアル化するためにビューに個別に送信する必要があったということです。ともあれ、ありがとう :)

于 2013-01-04T17:43:56.240 に答える