多个model时,可以通过在viewModel中添加各个model的属性,将多个model传到view里并能显示值,但却不能把这各个model返回给Controllers?请问,如何处理啊??
好吧,我把源码贴上:
有两个model类,其中一个是Address
public class Address : ValueObject<Address> { #region 属性 public string City { get; private set; } public string ZipCode { get; private set; } public string AddressLine1 { get; private set; } public string AddressLine2 { get; private set; } #endregion #region 构造函数 public Address(string city, string zipCode, string addressLine1, string addressLine2) { this.City = city; this.ZipCode = zipCode; this.AddressLine1 = addressLine1; this.AddressLine2 = addressLine2; } public Address() { } #endregion }
另一个是Customer类
public class Customer : IValidatableObject { #region 属性 /// <summary> /// 客户ID /// </summary> public string CustomerId { get; set; } /// <summary> /// 姓名1 /// </summary> [Required(ErrorMessage = "请输入First Name")] public string FirstName { get; set; } /// <summary> /// 姓名2 /// </summary> [Required(ErrorMessage = "请输入Last Name")] public string LastName { get; set; } /// <summary> /// 全名 /// </summary> public string FullName { get { return string.Format("{0}, {1}", this.LastName, this.FirstName); } set { } } //[RegularExpression("")] /// <summary> /// 电话 /// </summary> [Required(ErrorMessage = "请输入电话号码!")] public string Telephone { get; set; } /// <summary> /// 公司名称 /// </summary> [StringLength(20, ErrorMessage = "只允许输入20个字符")] public string Company { get; set; } /// <summary> /// /// </summary> public decimal CreditLimit { get; set; } public string CountryId { get; set; } public Address Address { get; set; } #endregion }
EditCustomerVM是ViewModel类
public class EditCustomerVM { public Customer Customer { get; set; } /// <summary> /// 公司 /// </summary> public string Company { get; set; } }
接下来是cshtml页面:EditCustomer.cshtml
@using Presentation.Web.Core @model Presentation.Web.ViewModels.CustomerViewModels.EditCustomerVM @{ ViewBag.Title = "EditCustomer"; if (IsAjax) { Layout = null; } } <div class="ui-frame-corner"> <li><span style="display: block;margin-left:10px;">编辑客户</span></li> <div class="accordion_content" id="query_form"> @using (Html.BeginForm("EditCustomer", "Customer", FormMethod.Post, new { id = "form1" })) { @Html.HiddenFor(m => m.Customer.CustomerId) <table width="100%" border="0" cellpadding="0" cellspacing="0" class="main_form"> <tr> <td class="main_form_td-align1 main_form_td-width1"> 名 </td> <td class="main_form_td-align2"> @Html.TextBoxFor(m => m.Customer.FirstName) @Html.ValidationMessageFor(m => m.Customer.FirstName) </td> <td class="main_form_td-align3"> 姓 </td> <td class="main_form_td-align4"> @Html.TextBoxFor(m => m.Customer.LastName) @Html.ValidationMessageFor(m => m.Customer.LastName) </td> </tr> <tr> <td class="main_form_td-align1 main_form_td-width1"> 电话号码 </td> <td class="main_form_td-align2"> @Html.TextBoxFor(m => m.Customer.Telephone) @Html.ValidationMessageFor(m => m.Customer.Telephone) </td> <td class="main_form_td-align3"> 公司 </td> <td class="main_form_td-align4"> @Html.TextBoxFor(m => m.Customer.Company) @Html.ValidationMessageFor(m => m.Customer.Company) </td> </tr> <tr> <td class="main_form_td-align3"> 城市: </td> <td class="main_form_td-align4"> @Html.TextBoxFor(m => m.Customer.Address.City) </td> <td class="main_form_td-align3"> 邮编: </td> <td class="main_form_td-align4"> @Html.TextBoxFor(m => m.Customer.Address.ZipCode, new { @class = "form_input" }) </td> </tr> <tr> <td class="main_form_td-align3"> 街道1: </td> <td class="main_form_td-align4"> @Html.TextBoxFor(m => m.Customer.Address.AddressLine1, new { @class = "form_input" }) </td> <td class="main_form_td-align3"> 街道2: </td> <td class="main_form_td-align4"> @Html.TextBoxFor(m => m.Customer.Address.AddressLine2, new { @class = "form_input" }) </td> </tr> </table> <div class="button_box"> <table align="center" cellpadding="0" cellspacing="0" class="accordion_content_2buttons"> <tr> <td align="center" valign="middle"> <table border="0" cellspacing="0" cellpadding="0"> <tr> <td> <input id="btnSubmit" type="button" class="btn-save-icon" value="保 存"/> </td> <td><input id="btnReturn" type="button" class="btn-return-icon" value="返 回"/> </td> </tr> </table> </td> </tr> </table> </div> } </div> </div> <script type="text/javascript"> $(function () { $("#btnSubmit").bind('click', function () { var form = $("#form1"); if (form.length > 0) { form.submit(); } }); //返回按钮Click事件 $("#btnReturn").bind('click', function () { window.location.href = "@Url.Action("QueryCustomer")"; }); }); </script>
以下是CustomerController,部分代码如下
/// <summary>
/// 得到单条记录数据(这个方法中Address是有值的)
/// </summary>
/// <param name="customerId"></param>
/// <returns></returns>
public ActionResult EditCustomer(string customerId)
{
Customer customer = _ICustomerRepository.Get(customerId);
EditCustomerVM vm = new EditCustomerVMBuilder().Bind();
vm.Customer = customer;
vm.Customer.Address = customer.Address;
vm.Company = customer.Company;
vm.LovesList = CommonModelBuilder.LovesList();
return View(vm);
}
}
有问题的方法:
/// <summary> /// 编辑数据方法 /// </summary> /// <param name="formCollection"></param> /// <returns></returns> [HttpPost] public ActionResult EditCustomer( FormCollection formCollection) { try { if (ModelState.IsValid) { Customer _Customer = new Customer(); Address _Address = new Address(); UpdateModel(_Customer, "Customer"); UpdateModel(_Address, "Address"); _ICustomerRepository.Update(_Customer); _ICustomerRepository.UnitOfWork.Commit(); } } catch (Exception ex) { ModelState.AddModelError("提交数据错误", ex); } return RedirectToAction("QueryCustomer"); }
就是在以上方法中,在_Customer中的Address值都是null,请问各大侠,是什么原因啊??
问题原因已找到:是因为Address中的属性设置了private set 了,多谢各位大侠关注,谢谢。
贴点代码~
看你的描述,这样的写法应该是不能自动装配的。
创建一个类 A,把多个model组合起来,然后 以A这个类作为 view和controllers之间的model进行传值
这个提问本身就是问题,怎么会存在从View中将Model返回给Controller的情况?
如修改一条数据,是需要将页面的值传给Controller的呀
????你要提交到各自指定的controller和action
一个view对应一个viewmodel