【发布时间】: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