【问题标题】:No view updating after viewmodel been changed更改视图模型后没有视图更新
【发布时间】:2012-10-26 21:26:18
【问题描述】:

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

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

【问题讨论】:

  • 你正在做一个 ajax 帖子,但你没有做任何事情来更新你对成功的看法;成功回调中只有“...?”。你是在问:ajax post的结果如何显示?
  • 是的。我不熟悉javascript。它应该以某种方式添加新的 @Html.EditorFor(item => item.Order.DomenNames[i]) 元素。
  • 我建议研究一下 Razor 的 Ajax 助手,它对这类东西很有用。
  • 您使用 AJAX 而不是整页回发是否有特殊原因?看起来你想显示一个完全不同的视图,你只回帖和重定向不是更好吗?
  • 也许我错过了一些东西,但我不想使用 POSTback 验证其他字段...我不想在这个阶段对这些字段进行验证。应在提交表单时进行验证(不是在“查看”代码中编写的)...

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


【解决方案1】:

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

控制器:

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

希望对大家有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    相关资源
    最近更新 更多