模型中的模型 - 在下拉列表中选择项目?

Model in model - selecting item in dropdown list?

所以我遇到了一个问题,我想在一个视图中使用多个模型,为了完成这项工作,我让视图使用了一个包含所有 3 个模型的模型。

LogStatisticsModel.cs

namespace ASD.Models
{
    public class LogStatisticsModel
    {
        public LogModel LogModelObject { get; set; }
        public StatisticsModel StatisticsModelObject { get; set; }
        public LogModelVM LogModelVMObject { get; set;}
    }
}

现在,当我在视图上使用 LogModelVM 时,它仍然可以正常工作,除了一个小细节,下拉列表的值不再遵循名称 "SelectedCustomer" 下的控制器。但它现在尝试发送 "LogModelVMObject.SelectedCustomer"。因此传递的值为空..

_LogLayout.cshtml

@model ASD.Models.LogStatisticsModel

@using (Ajax.BeginForm("LogPartialView", "LogModelsController",
       new AjaxOptions
       {
             HttpMethod = "POST",
             InsertionMode = InsertionMode.Replace,
             UpdateTargetId = "divLogs",
       }, new
       {
          id = "NewTableId"
       }))
       {

       <p>@Html.TextBox("SearchString",null, new { @placeholder = "Message" })</p>
       if (Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin"))
       {
       <p>
       @Html.DropDownListFor(m => m.LogModelVMObject.SelectedCustomer, Model.LogModelVMObject.CustomerList, new {@id = "logdropdownlabel" })
       </p>
       }

       <p><input type="submit" class="standardbutton logsearch" name="submit" value="Search" /></p> 
       }

LogModelsController.cs

[HttpPost]
        public PartialViewResult LogPartialView(string searchString, int? selectedCustomer, string currentMessageFilter, string currentCustomerFilter, int? page, string sortOrder)
                LogModelVM LMVM = new LogModelVM();
                Customer = GetCustomerIds();
                LMVM.CurrentSort = sortOrder;

                if (Session["myID"] != null)
                {
                    myID = Session["myID"].ToString();
                }
                else
                {
                    myID = selectedCustomer.ToString();
                }
                try
                {
                    testID = Convert.ToInt32(myID);
                }
                catch (FormatException ex)
                {
                    throw new FormatException("FormatException error in LogModelsController", ex);
                }

                if (searchString != null)
                {
                    page = 1;
                }
                else
                {
                    searchString = currentMessageFilter;
                    selectedCustomer = Convert.ToInt32(currentCustomerFilter);
                }

                LMVM.CurrentMessageFilter = searchString;
                LMVM.CurrentCustomerFilter = Convert.ToInt32(selectedCustomer);

                if ((String.IsNullOrEmpty(searchString) && !String.IsNullOrEmpty(selectedCustomer.ToString())))
                {
                    //  Search a Customer ID but no message
                    //  Search a Customer ID but no message, change page
                    if (selectedCustomer != 0)
                        message = db.GetLogsById(Convert.ToInt32(selectedCustomer));
                    //  You have not searched and change page
                    else
                        message = db.GetLogs();
                }
                //  Searched Customer ID and message
                //  Searched Customer ID and message, change paged
                else if (!String.IsNullOrEmpty(searchString) && !String.IsNullOrEmpty(selectedCustomer.ToString()))
                {
                    testID = Convert.ToInt32(selectedCustomer);
                    Session["myID"] = testID.ToString();
                    message = db.GetLogsById(testID).Where(s => s.message.Contains(searchString));
                }
                else
                {
                    message = db.GetLogs();
                }

                Session["mySearchString"] = searchString;
                Session["mySearchString2"] = selectedCustomer;

                //PageSize displays the maximum number of rows on each page
                int pageSize = 10;
                int pageNumber = (page ?? 1);
                var logs = message.OrderByDescending(i => i.timeStamp).ToPagedList(pageNumber, pageSize);
                foreach (var log in logs)
                    log.Name = Customer.Where(a => a.Value == log.customerId.ToString()).FirstOrDefault().Text;
                LMVM.Logs = logs;
                LMVM.CustomerList = Customer;

                var model = new LogStatisticsModel
                {
                    LogModelObject = new LogModel(),
                    StatisticsModelObject = new StatisticsModel(),
                    LogModelVMObject = LMVM
                };

                return PartialView("_LogPartialLayout", model);
        }

现在应该如何获取控制器的值?我假设我不能在控制器的函数中写 "int? LogModelVMObject.SelectedCustomer"。

您可以像这样重写 Html.DropdownList:

@Html.DropDownList("SelectedCustomer", Model.LogModelVMObject.CustomerList, new { @id = "logdropdownlabel" })

通过手动输入名称作为第一个参数,当您 post 将数据返回控制器时,该名称应该在请求表单中。