gpt4 book ai didi

c# - MVC4 : View appears to be maintaining state independently of the controller

转载 作者:太空宇宙 更新时间:2023-11-03 13:33:46 35 4
gpt4 key购买 nike

我有一个下拉列表(客户)和复选框列表(销售订单),具体取决于当前选择的客户。如果我选择一个新客户,我希望复选框会被清除,但是尽管在回发中清除了模型,但它们会从一个到另一个保持不变。

我不是经验丰富的 MVC 开发人员,但我不确定为什么会这样。在调试我发送回 View 的 ViewModel 时,它显示所有复选框的 IsSelected = false,但在 View 中,它们被选中。我究竟做错了什么? (提前致谢)

查看模型:

namespace MvcTest1.Models
{
public class Customer
{
public int CustomerID { get; set; }
public string Name { get; set; }
}

public class SalesOrder
{
public int SalesOrderID { get; set; }
public string Reference { get; set; }
public bool IsSelected { get; set; }
}

public class SalesOrderPageViewModel
{
public List<Customer> Customers { get; set; }
public int SelectedCustomerID { get; set; }
public List<SalesOrder> SalesOrders { get; set; }
}
}

Controller :

namespace MvcTest1.Controllers
{
public class SalesOrderPageController : Controller
{
[HttpGet]
public ActionResult Index()
{
SalesOrderPageViewModel viewModel = BuildViewModel(1);

return View(viewModel);
}

[HttpPost]
public ActionResult Index(SalesOrderPageViewModel viewModelInput)
{
SalesOrderPageViewModel viewModel = BuildViewModel(viewModelInput.SelectedCustomerID);

return View(viewModel);
}

public SalesOrderPageViewModel BuildViewModel(int customerID)
{
SalesOrderPageViewModel viewModel = new SalesOrderPageViewModel
{
Customers = new List<Customer>
{
new Customer { CustomerID = 1, Name = "Alan" },
new Customer { CustomerID = 2, Name = "Bob" },
new Customer { CustomerID = 3, Name = "Charlie" }
}
};

viewModel.SelectedCustomerID = customerID;

if (customerID == 1)
{
viewModel.SalesOrders = new List<SalesOrder>
{
new SalesOrder { SalesOrderID = 11, Reference = "AA11" },
new SalesOrder { SalesOrderID = 12, Reference = "AA22" },
new SalesOrder { SalesOrderID = 13, Reference = "AA33" }
};
}
if (customerID == 2)
{
viewModel.SalesOrders = new List<SalesOrder>
{
new SalesOrder { SalesOrderID = 21, Reference = "BB11" },
new SalesOrder { SalesOrderID = 22, Reference = "BB22" },
new SalesOrder { SalesOrderID = 23, Reference = "BB33" }
};
}
if (customerID == 3)
{
viewModel.SalesOrders = new List<SalesOrder>
{
new SalesOrder { SalesOrderID = 31, Reference = "CC11" },
new SalesOrder { SalesOrderID = 32, Reference = "CC22" },
new SalesOrder { SalesOrderID = 33, Reference = "CC33" }
};
}

return viewModel;
}
}
}

查看:

@model MvcTest1.Models.SalesOrderPageViewModel

@{
ViewBag.Title = "SalesOrderPage";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>SalesOrderPage</h2>

<br /><br />

@using (Html.BeginForm())
{

@Html.DropDownListFor(model =>
model.SelectedCustomerID,
new SelectList(
Model.Customers,
"CustomerID",
"Name",
Model.SelectedCustomerID
),
new { id = "customerSelect" }
);

<script type="text/javascript">

$(function () {
$('#customerSelect').change(function () {
this.form.submit();
});
})

</script>

<br /><br />

for (int i = 0; i < Model.SalesOrders.Count(); i++)
{
@Html.DisplayFor(m => Model.SalesOrders[i].Reference)
@Html.CheckBoxFor(m =>
m.SalesOrders[i].IsSelected
)
<br />
}

}

最佳答案

看起来 Ryan 对 ModelState.Clear() 的看法是正确的。这是一篇解释为什么有必要的文章:

http://patrickdesjardins.com/blog/modelstate-clear-is-required-to-display-back-your-model-object

关于c# - MVC4 : View appears to be maintaining state independently of the controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19451291/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com