So I had a problem where I wanted to use multiple models in a view, and to make this work I made the view use a model which contained all the 3 models.

LogStatisticsModel.cs

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

Now to start off the LogModelVM still works perfectly when I use it on the view except for one small detail, the value of the dropdown doesn't follow to the controller under the name "SelectedCustomer" anymore. But it tries to send a "LogModelVMObject.SelectedCustomer" now instead. Thus the value passed is null..

enter image description here

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

How should I get the value to the controller now? I assume I can't write "int? LogModelVMObject.SelectedCustomer" in the function in the controller.

Related posts

Recent Viewed