我不确定如何才能让它发挥作用。我不知道如何为此ViewModel上的[Display(Name = "XXXX")]
属性创建Customer
:
public class CreateAccountViewModel
{
[Required]
public Customer Customer { get; set; }
[Required]
[Display(Name = "Username")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
public string ConfirmPassword { get; set; }
}
我应该为ViewModel
创建Customer
并引用而不是Customer
对象吗?然后为所有属性创建Display()
?
如果是这样,映射是否可以提交?
我使用如下(片段):
<div class="form-group">
@Html.LabelFor(m => m.Customer.CompanyName, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Customer.CompanyName, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Customer.CVR, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Customer.CVR, new { @class = "form-control" })
</div>
</div>
而“公司名称”我希望改成更像“公司名称”的东西。 : - )
答案 0 :(得分:4)
像往常一样,如果我需要显示一些带有这些子对象的复杂模型 - 我为这个对象创建了另一个模型,在新模型中指定属性。然后指定此模型而不是Customer属性。
@Html.LabelFor(m => m.Customer.CompanyName, new { @class = "col-md-2 control-label" })
如果子模型具有“显示”属性,则应该可以正常工作。
另一种方法是在你的域类下指定这些属性,但就我而言 - 我不喜欢具有UI属性的脏域对象
答案 1 :(得分:0)
哟可以使用这个结构:
[DisplayName("YourName")]
public string UserName { get; set; }
并在您看来:
@Html.DisplayNameFor(m => m.Customer.CompanyName, new { @class = "col-md-2 control-label" }))
答案 2 :(得分:0)
好的,我做了一个不同的方法。我不确定这是否更优雅。根据我的研究,我遇到了AutoMapper并完成了这项工作。如果有人对我的问题有更好的解决方案,请告诉我。 : - )
public static class AutoMapperWebConfiguration
{
public static void Configure()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile(new CustomerMapper());
cfg.AddProfile(new CustomerAddressMapper());
});
}
}
public class CustomerMapper : Profile
{
protected override void Configure()
{
Mapper.CreateMap<CustomerViewModel, Customer>().ForMember(customer => customer.Address, expression => expression.MapFrom(viewModel => viewModel.Address));
}
}
public class CustomerAddressMapper : Profile
{
protected override void Configure()
{
Mapper.CreateMap<CustomerAddressViewModel, Address>();
}
}
<强>的Global.asax:强>
AutoMapperWebConfiguration.Configure();
我的ViewModels
就是
public class CreateAccountViewModel
{
public CustomerViewModel Customer { get; set; }
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
public string ConfirmPassword { get; set; }
}
public class CustomerViewModel
{
[Required]
[Display(Name = "Company Name")]
public String CompanyName { get; set; }
[Required]
[Display(Name = "CVR")]
public String CVR { get; set; }
[Required]
[Display(Name = "First name")]
public String FirstName { get; set; }
[Required]
[Display(Name = "Last name")]
public String LastName { get; set; }
[Required]
[Display(Name = "Phone (mobile)")]
public String Phone { get; set; }
[Required]
[Display(Name = "E-mail")]
public String Email { get; set; }
public CustomerAddressViewModel Address { get; set; }
}
public class CustomerAddressViewModel
{
[Required]
[Display(Name = "Streey")]
public String Street { get; set; }
[Required]
[Display(Name = "Postal code")]
public String PostalCode { get; set; }
[Required]
[Display(Name = "City")]
public String City { get; set; }
[Required]
[Display(Name = "Country")]
public String Country { get; set; }
}
最后是我的action
,其中CreateAccountViewModel
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(CreateAccountViewModel model)
{
if (ModelState.IsValid) {
Customer customer = Mapper.Map<Customer>(model.Customer);
var user = new ApplicationUser() { UserName = model.UserName, Customer = customer };
var result = UserManager.Create(user, model.Password);
if (result.Succeeded)
{
await SignInAsync(user, false);
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
}
// If we got this far, something failed, redisplay form
return View(model);
}