I am unsure how I can get this to work. I dont know how I can create [Display(Name = "XXXX")]
for my Customer
properties on this ViewModel:
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; }
}
Should I create a ViewModel
for Customer
and reference that instead of the Customer
object? And then create Display()
for all of the properties?
If so, will the mapping work on submit?
I use this as follows (snippet):
<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>
And the "CompanyName" I wish to change to something more nice like "Company name". :-)
As usual if I need to display some complex model with such sub objects - I create another model for this object, specify attributes in new model. And then specify this model instead of Customer property.
@Html.LabelFor(m => m.Customer.CompanyName, new { @class = "col-md-2 control-label" })
Should work fine for sub models, if it has Display attribute.
Another way is to specify these attributes under your domain classes, but as for me - i don't like to dirty domain objects with UI attributes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With