找不到问题所在,也无法在任何地方找到答案。我的问题是视图模型更改 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>
将模型传递给控制器很好,在通过视图进行调试时,我可以看到模型已更改,但在某些情况下,视图中什么也没有发生。看起来旧模型已经到位。