2

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

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

查看型号:

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

控制器:

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 />
    }

}
4

1 回答 1

1

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

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

于 2013-10-18T13:56:31.217 回答