Asp.net mvc ASP.NET MVC中重载的ViewModel构造函数和HttpPost
我在StackOverflow中找到的最接近我的问题是 型号Asp.net mvc ASP.NET MVC中重载的ViewModel构造函数和HttpPost,asp.net-mvc,asp.net-mvc-4,razor,asp.net-mvc-5,Asp.net Mvc,Asp.net Mvc 4,Razor,Asp.net Mvc 5,我在StackOverflow中找到的最接近我的问题是 型号 public class Customer { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } public class CustomerViewModel
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class CustomerViewModel
{
public Customer Customer { get; set; }
public CustomerViewModel(Customer customer)
{
Customer = customer;
}
}
视图模型
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class CustomerViewModel
{
public Customer Customer { get; set; }
public CustomerViewModel(Customer customer)
{
Customer = customer;
}
}
控制器代码
public ActionResult CreateCustomer()
{
Customer c = new Customer();
CustomerViewModel cvm = new CustomerViewModel(c);
return View(cvm);
}
[HttpPost]
public ActionResult CreateCustomer(CustomerViewModel customer)
{
// do something here
}
@model Blah.Models.CustomerViewModel
@{
ViewBag.Title = "CreateCustomer";
}
<h2>CreateCustomer</h2>
@using (Html.BeginForm())
{
<div class="editor-label">
@Html.LabelFor(model => model.Customer.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Customer.FirstName)
@Html.ValidationMessageFor(model => model.Customer.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Customer.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Customer.LastName)
@Html.ValidationMessageFor(model => model.Customer.LastName)
</div>
<p>
<input type="submit" value="Create" />
</p>
}
查看代码
public ActionResult CreateCustomer()
{
Customer c = new Customer();
CustomerViewModel cvm = new CustomerViewModel(c);
return View(cvm);
}
[HttpPost]
public ActionResult CreateCustomer(CustomerViewModel customer)
{
// do something here
}
@model Blah.Models.CustomerViewModel
@{
ViewBag.Title = "CreateCustomer";
}
<h2>CreateCustomer</h2>
@using (Html.BeginForm())
{
<div class="editor-label">
@Html.LabelFor(model => model.Customer.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Customer.FirstName)
@Html.ValidationMessageFor(model => model.Customer.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Customer.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Customer.LastName)
@Html.ValidationMessageFor(model => model.Customer.LastName)
</div>
<p>
<input type="submit" value="Create" />
</p>
}
@model Blah.Models.CustomerViewModel
@{
ViewBag.Title=“CreateCustomer”;
}
创建客户
@使用(Html.BeginForm())
{
@LabelFor(model=>model.Customer.FirstName)
@EditorFor(model=>model.Customer.FirstName)
@Html.ValidationMessageFor(model=>model.Customer.FirstName)
@LabelFor(model=>model.Customer.LastName)
@EditorFor(model=>model.Customer.LastName)
@Html.ValidationMessageFor(model=>model.Customer.LastName)
}
错误
仅消除错误但没有帮助的解决方案
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class CustomerViewModel
{
public Customer Customer { get; set; }
public CustomerViewModel(Customer customer)
{
Customer = customer;
}
}
- 添加默认构造函数(参数为空-不符合我的目的)
- 不要使用重载构造函数(我的模型将是空的)
我想知道我这里还有哪些选项您需要添加无参数构造函数
public class CustomerViewModel
{
public Customer Customer { get; set; }
public CustomerViewModel()
{
}
public CustomerViewModel(Customer customer)
{
Customer = customer;
}
}
您认为这“不起作用”的原因是另一个问题。您的模型有一个名为Customer
的属性,它是一个复杂类型,并且POST方法的参数也被命名为Customer
(DefaultModelBinder
不区分大小写)。因此,绑定失败。例如,您需要将参数名称更改为除某个属性名称以外的任何名称
[HttpPost]
public ActionResult CreateCustomer(CustomerViewModel model)
这个人遇到的问题非常相似:尝试在
CustomerServiceWModel
中添加无参数构造函数,因为AFAIK MVC需要默认的无参数构造函数。同时使用无参数构造函数和带有Customer
类参数的构造函数有什么问题?将无参数构造函数添加为以及您现有的构造函数。您的模型将不会为空。如果您有绑定到Customer
属性的控件,则Customer
将在回发时正确绑定。您当然不需要自定义模型绑定器。