在使用 ASP.NET MVC 3 的 Web 应用程序中,我从控制器将具有初始化属性的模型作为参数传递给局部视图。
该视图显示一个带有单个文本框的对话框,并在提交时触发启动控制器中的操作(该操作采用与参数相同的模型类型)。
问题是此时只有相对于文本框字段的属性具有值,即用户插入的值,而所有其他属性都为空,即使在视图中它们具有适当的值。
单击提交按钮后,如何才能将属性从视图保留到控制器?
编辑(添加代码):
//---------- This method in the controller call the Partial View and pass the model --------
[HttpPost]
public PartialViewResult GetAddCustomFormerClubDialog()
{
var order = GetCurrentOrder();
//Order has here all properties initialized
var dialogModel = new dialogModel<Order> { Entity = order, ControllerAddEntityActionName = "SelectOrder"};
return PartialView("Dialogs/AddOrder", dialogModel);
}
//----------------- Here the Partial View -----------------------------------
@model FifaTMS.TMS.Presentation.Model.Wizard.WizardDialogModel<Club>
<div>
@using (Ajax.BeginForm(Model.ControllerAddEntityActionName, "Orders", new AjaxOptions { HttpMethod = "POST"}))
{
@Html.LabelFor(a => a.Entity.Name)
@Html.TextBoxFor(a => a.Entity.Name, new { @class = "isrequired", style="width: 250px;" })
}
</div>
//-------- Here the method from the view (in the same controller as the first code portion) -----
[HttpPost]
public JsonResult SelectOrder(dialogModel<Order> OrderModel)
{
var order= OrderModel.Entity;
// But order has only the property Name set (in the view)
...
}