所以我有一個問題,我想在視圖中使用多個模型,爲了使這個工作成爲可能,我使用了一個包含所有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」。因此,傳遞的值爲null ..
_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」。