0

找不到问题所在,也无法在任何地方找到答案。我的问题是视图模型更改 bean 后我的视图没有更新

视图模型:

public class OrderView
{
    public Customer Customer { get; set; }
    public Order Order { get; set; }
}
public class Order
{
    public int OrderId { get; set; }
    public int CustomerId { get; set; }
    public List<string> DomenNames { get; set; }
}
public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Telephone { get; set; }
    public string Email { get; set; }
}

控制器:

private OrderView ov;
public ActionResult Index()
{
    return View(ov);
}

[HttpPost]
public ActionResult Index(OrderView model, FormCollection collection) {            
    return View("done");
}
public ActionResult BlankEditorRow(OrderView model) {
    ov = model;
    ov.Order.DomenNames.Add("");
    return View("Index",ov) ;
}

看法:

@using (Html.BeginForm("Index","Order",FormMethod.Post, new {id = "createOrder"})) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Order</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Order.DomenNames)
    </div>

    @for(int i = 0; i < Model.Order.DomenNames.Count; i++) {
        <div> 
            @Html.EditorFor(item => item.Order.DomenNames[i])
        </div>
    }

    <button type="button" id="b1" onclick="setallert()" >Click me</button>
 ...

和脚本:

<script type="text/javascript">
    function setallert() {
        $.ajax({
            url: "Order/BlankEditorRow",
            data: $('#createOrder').serialize(),
            cache: false,
            success: function (data) {
                ...?
            }
        });
    };    
</script>

将模型传递给控制器​​很好,在通过视图进行调试时,我可以看到模型已更改,但在某些情况下,视图中什么也没有发生。看起来旧模型已经到位。

4

1 回答 1

0

我已经解决了我的问题。我发布我的解决方案以防有人需要它:

控制器:

public ActionResult BlankEditorRow(OrderView model) {
    model.Order.DomenNames.Add("");
    return PartialView("Index", model) ;
}

看法:

<div class="editor-label">
    @Html.LabelFor(model => model.Order.DomenNames)
</div>
<div id="tt">
    @{Html.RenderPartial("OrderDN", this.Model);}
</div>
<button type="button" id="addBtn">Click me</button>

脚本:

$("#addBtn").click(function () {
    $.get('@Url.Action("BlankEditorRow", "Order")', $('#createOrder').serialize(), function (res) {
        document.getElementById("tt").innerHTML = res;
    });
})

我希望它对某人有用

于 2012-10-29T19:34:05.557 回答