C# 与ASP.NET中的html帮助程序混淆

C# 与ASP.NET中的html帮助程序混淆,c#,asp.net,html-helper,C#,Asp.net,Html Helper,我有一个视图模型课 public class NewCustomerViewModel { public IEnumerable<MembershipType> MembershipType { get; set; } public Customer Customer { get; set; } } 意见是: @using (Html.BeginForm("Create", "Customers")) { <div class="form-group"&

我有一个视图模型课

public class NewCustomerViewModel
{
   public IEnumerable<MembershipType> MembershipType { get; set; }
   public Customer Customer { get; set; }
}
意见是:

@using (Html.BeginForm("Create", "Customers"))
{ 
    <div class="form-group">
        @Html.LabelFor(e => e.Customer.Name)
        @Html.TextBoxFor(e => e.Customer.Name, new { @class = "form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(e => e.Customer.Birthdate)
        @Html.TextBoxFor(e => e.Customer.Birthdate, new { @class = "form-control" })
    </div>
    <div class="checkbox">
        <label>
           @Html.CheckBoxFor(e => e.Customer.IsSubscribedToNewsletter, new { @class = "checkbox" }) Subscribed To Newsletter?
        </label>
    </div>
我们现在只有一个空的Customer实例,我们正在尝试获取名称?

我刚才有过

尽管您从未实例化客户,但对象已定义,引擎可以为其构建视图


在回发时,ModelBinder将实例化一个新的
客户
,并尝试从您的表单值中备份它。web是无状态的,因此在构建表单时,无论您发送的是预填充的
客户对象还是空对象,ASP.NET都只能从表单中的内容构建表单。

您的问题真的不清楚。我不完全理解您的问题,你能不能试着把你说的话精炼一下,并用一个问题明确地表达出来。另外,让我们知道您希望发生什么您可能需要在您的模型中创建一个新的Customer对象,目前它是空的。“我对html helper不是很困惑”。。。好吧,那很好,对吗?@SimonPrice抱歉,我已经重新表述了我的问题,因为有两个客户对象?一个为空,一个以表单值作为属性?我觉得很奇怪,因为只有一个空的Customer对象什么都不做?在
GET
请求中,有一个对象包含一个空的
Customer
对象,但仍然有关于
Customer
的元数据。回发时,ModelBinder将实例化
Customer
对象,并尝试为控制器填充该对象。html帮助程序(
@html.TextBoxFor(e=>e.Customer.Name,…
)正在获取有关
Customer.Name
对象的元数据,以便为该输入创建标签
@using (Html.BeginForm("Create", "Customers"))
{ 
    <div class="form-group">
        @Html.LabelFor(e => e.Customer.Name)
        @Html.TextBoxFor(e => e.Customer.Name, new { @class = "form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(e => e.Customer.Birthdate)
        @Html.TextBoxFor(e => e.Customer.Birthdate, new { @class = "form-control" })
    </div>
    <div class="checkbox">
        <label>
           @Html.CheckBoxFor(e => e.Customer.IsSubscribedToNewsletter, new { @class = "checkbox" }) Subscribed To Newsletter?
        </label>
    </div>
@Html.TextBoxFor(e => e.Customer.Name, ...)