C# Asp.net MVC发布表单空值

C# Asp.net MVC发布表单空值,c#,asp.net-mvc,C#,Asp.net Mvc,我目前正在学习Mosh的asp.NETMVC课程 最近我开始使用表单发布数据,但我遇到了一个无法解决的问题 因此,添加客户的表单使用包含可用的成员类型的newCustomerServiceWModel。然后,在表单中,我使用下拉列表选择一个,并将其与此客户绑定 但是,在提交之后,Customer中的MembershipType设置为null,尽管MembershipTypeID已正确绑定 我不知道如何解决这个问题。任何建议都将不胜感激。先谢谢你 代码: 客户类别: public class Cu

我目前正在学习Mosh的asp.NETMVC课程

最近我开始使用表单发布数据,但我遇到了一个无法解决的问题

因此,添加客户的表单使用包含可用的
成员类型的
newCustomerServiceWModel
。然后,在表单中,我使用下拉列表选择一个,并将其与此客户绑定

但是,在提交之后,Customer中的
MembershipType
设置为
null
,尽管
MembershipTypeID
已正确绑定

我不知道如何解决这个问题。任何建议都将不胜感激。先谢谢你

代码:

客户类别:

public class Customer
{
    public int ID { get; set; }
    [Required]
    [StringLength(70)]
    public String Name { get; set; }
    public bool IsSubscribedToNewsletter { get; set; }
    public MembershipType MemebershipType { get; set; }
    public int MembershipTypeID { get; set; }
    public DateTime? Birthdate { get; set; }
}
客户控制员:

public class CustomerController : Controller
{
    private ApplicationDBContext context;

    public CustomerController(ApplicationDBContext ctxt)
    {
        this.context = ctxt;
    }

    public CustomerController()
    {
        this.context = new ApplicationDBContext();
    }

    protected override void Dispose(bool disposing)
    {
        context.Dispose();
    }
    // GET: Customer
    public ActionResult Index()
    {

        var model = context.Customers.Include(x => x.MemebershipType).ToList();
        return View(model);
    }

    public ActionResult Details(int id)
    {
        var ct = context.Customers.Include(c => c.MemebershipType).SingleOrDefault(c => c.ID == id);
        if (ct == null) return HttpNotFound();
        return View(ct);
    }

    public ActionResult New()
    {
        var membershiptypes = context.MembershipTypes.ToList();
        var viewmodel = new NewCustomerViewModel
        {
            MembershipTypes = membershiptypes
        };
        return View(viewmodel);
    }
    [HttpPost]
    public ActionResult Create(Customer customer)
    {
        context.Customers.Add(customer);
        context.SaveChanges();
        return RedirectToAction("Index", "Customer");
    }
}
新客户服务模型

public class NewCustomerViewModel
{
    public IEnumerable<MembershipType> MembershipTypes { get; set; }
    public Customer Customer { get; set; }
}
公共类newCustomerServiceWModel
{
公共IEnumerable成员身份类型{get;set;}
公共客户客户{get;set;}
}
用于添加新客户的视图:

    @model Vidly.ViewModels.NewCustomerViewModel
@{
    ViewBag.Title = "New";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>New Customer</h2>

@using (Html.BeginForm("Create", "Customer"))
{
    <div class="form-group">
        @Html.LabelFor(c => c.Customer.Name)
        @Html.TextBoxFor(c => c.Customer.Name, new { @class = "form-control" })
    </div>
    <div class="form-group">
        <label>Date of birth</label>
        @Html.TextBoxFor(c => c.Customer.Birthdate, new { @class = "form-control" })
    </div>
    <div class="checkbox">
        <label>
            @Html.CheckBoxFor(c => c.Customer.IsSubscribedToNewsletter) Subscribed to newsletter?
        </label>
    </div>
    <div class="form-group">
        <label>Membership type</label>
        @Html.DropDownListFor(c => c.Customer.MembershipTypeID,
       new SelectList(Model.MembershipTypes, "ID", "Name"), "Select membership type", new {@class="form-control"})
    </div>

    <button type="submit" class="btn btn-primary">
        Save
    </button>
}  
@model Vidly.ViewModels.NewCustomerViewModel
@{
ViewBag.Title=“新建”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
新客户
@使用(Html.BeginForm(“创建”、“客户”))
{
@LabelFor(c=>c.Customer.Name)
@TextBoxFor(c=>c.Customer.Name,新的{@class=“form control”})
出生日期
@TextBoxFor(c=>c.Customer.Birthdate,new{@class=“form control”})
@Html.CheckBoxFor(c=>c.Customer.IsSubscribedToNewsletter)订阅时事通讯?
成员类型
@Html.DropDownListFor(c=>c.Customer.MembershipTypeID,
新建选择列表(Model.MembershipTypes,“ID”,“Name”),“选择成员类型”,新建{@class=“form control”})
拯救
}  

这是正常的,因为您没有设置
成员身份类型
,而是设置了
成员身份类型ID

当它保存在数据库中时,
MembershipTypeId
被保存

然后,当您通过“详细信息”操作查询该客户时,您将正确填充
成员身份类型

或者,您可以使用
contextManager

简单地说

customer.MembershipType = context.MembershipTypes.SingleOrDefault(m => m.id == customer.MembershipTypeId);

但是这将花费你不必要的时间来访问数据库。

谢谢@J.Scott的编辑……现在看起来好多了,我该怎么办?我将
@Html.DropDownListFor(c=>c.Customer.MemebershipTypeID,
更改为
@Html.DropDownListFor(c=>c.Customer.MemebershipType,
我仍然得到了相同的结果problem@Jania将代码更改回c=>c.Customer.MembershipTypeId
,一切都将正常运行,
MembershipType
null
,并在
操作中添加使用其
Id重新填充
MembershipType
你,我终于明白了,它开始正常工作了