在Telerik MVC Grid - Custom Command中序列化类型对象时检测到循环引用

时间:2023-01-19 20:54:00

I´m faced with what is probably just a small oversight on my behalf, but I´m trying to serialize a complex class in my View. I keep getting a circular reference when running this code.

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

Controller:

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>

View + Telerik:

查看+ 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>

My goal is to be able to press the "Price" button in the grid and a window pops up that allows me to change the price. The thing is that I´ve already been able to use this code and send a simple string to the view and that works perfectly, for instance sending "cat.Name" from my controller instead of only "cat", so basically when I send the complex type "Category" I get the circular reference.
Can any1 spot my failure? :)

我的目标是能够按下网格中的“价格”按钮,弹出一个窗口,允许我更改价格。问题是我已经能够使用这个代码并向视图发送一个简单的字符串,并且工作正常,例如从我的控制器发送“cat.Name”而不是“cat”,所以基本上当我发送复杂类型“类别”我得到循环引用。任何人都可以发现我的失败吗? :)

1 个解决方案

#1


0  

I belive I will answer my own question. Shortly after I posted the question I tried something else and it worked, like this:

我相信我会回答我自己的问题。在我发布问题后不久,我尝试了其他的东西并且它有效,如下所示:

Controller:

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

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

The thing is that I just had to take the bits from my complex type and send it separately to the view to be serialized there. Thank you anyway :)

问题是我只需从我的复杂类型中取出这些位并将其单独发送到要在那里序列化的视图。还是非常感谢 :)

#1


0  

I belive I will answer my own question. Shortly after I posted the question I tried something else and it worked, like this:

我相信我会回答我自己的问题。在我发布问题后不久,我尝试了其他的东西并且它有效,如下所示:

Controller:

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

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

The thing is that I just had to take the bits from my complex type and send it separately to the view to be serialized there. Thank you anyway :)

问题是我只需从我的复杂类型中取出这些位并将其单独发送到要在那里序列化的视图。还是非常感谢 :)