我已经阅读了一些关于 post-redirect-get 设计模式的内容,但我不确定它是否适合我的目的,因为我拥有的是一个 MVC 网站,它的设计看起来像一个应用程序,我在页面上有多个下拉菜单在我的控制器中全部绑定到一个整数数组,如下所示:
[HttpPost]
public ViewResult ResponseForm(PartyInvites.Models.GuestResponse response, int[] SelectedCustomer)
{
return View(response); // works but resets all my selected dropdowns
// return View(); // gives an error that it can't rebind items in view
}
我的观点:
@foreach (Schedule sched in Model.Schedules)
{
@Html.DropDownList("MySelectedCustomer", new SelectList(sched.Customers, "Id", "FirstName"), "Select A Customer", new { @class = "SelectedCustomer" })
}
客人反应:
public class GuestResponse
{
[Required(ErrorMessage = "You must enter your name")]
public string Name { get; set; }
public string SomeString = "someString";
public string Email { get; set; }
public string Phone { get; set; }
public bool? WillAttend { get; set; }
public int SelectedSchedule = 0;
public int SelectedCustomer = 0;
public List<Schedule> Schedules
{
get
{
return new List<Schedule>() { new Schedule() { ScheduleName = "party1", ScheduleId = 1 }, new Schedule() { ScheduleId = 2, ScheduleName = "party2" } };
}
set
{
Schedules = value;
}
}
}
SelectCustomer 属性是 GuestResponse 类的属性。所有的下拉列表都是绑定的,如果我更改了一些,它们会很好地绑定到 int[] SelectedCustomer 集合。但是我想返回我的视图(所以它本质上什么都不做)但这会将所有下拉列表重置为其原始状态,因为响应从未完全绑定,因为有多个下拉列表并且 MVC 无法对其进行模型绑定。这样做的最佳方法是什么,可以这么说来保持状态?