具有复杂属性的ViewModel

jan*_*ann 6 c# asp.net asp.net-mvc viewmodel

我不确定如何让这个工作.我不知道如何在这个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; }
}
Run Code Online (Sandbox Code Playgroud)

我应该创建一个ViewModelfor Customer和reference而不是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>
Run Code Online (Sandbox Code Playgroud)

而"公司名称"我希望改为更像"公司名称"的更好的东西.:-)

Sae*_*eed 0

你可以使用这个结构:

[DisplayName("YourName")]
public string UserName { get; set; }
Run Code Online (Sandbox Code Playgroud)

在你看来:

@Html.DisplayNameFor(m => m.Customer.CompanyName, new { @class = "col-md-2 control-label" }))
Run Code Online (Sandbox Code Playgroud)