viewmodel更改后没有视图更新

时间:2012-10-26 21:26:18

标签: asp.net-mvc-3 razor asp.net-ajax

无法找到什么错误,无法在任何地方找到答案。我的问题是我的视图在视图模型已更改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>

将模型传递给控制器​​并且在通过视图调试时我可以看到模型已更改,但在某些情况下,它在视图中没有任何变化。看起来旧模型已经到位。

1 个答案:

答案 0 :(得分: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;
    });
})

我希望它对某人有用