0

我面临的可能只是我的一个小疏忽,但我正试图在我的视图中序列化一个复杂的类。运行此代码时,我不断收到循环引用。

控制器:

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

查询:

<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>

查看 + Telerik:

<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.Name”而不是只发送“cat”,所以基本上当我发送复杂类型“类别”我得到循环引用。
任何人都可以发现我的失败吗?:)

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);
}

查询:

<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 回答