简体   繁体   English

Asp.net MVC 发布表单空值

[英]Asp.net MVC post form null value

I am currently following Mosh's asp.net mvc course.我目前正在学习 Mosh 的 asp.net mvc 课程。

Recently I got to using form to post data, but I encountered a problem that I cannot solve.最近开始用form来发数据,但是遇到了无法解决的问题。

So the form for adding a Customer uses NewCustomerViewModel that contains available MembershipTypes .因此,用于添加 Customer 的表单使用包含可用MembershipTypes NewCustomerViewModel Then, in the form I use a drop down list to select one and bind it with this Customer.然后,在表单中,我使用下拉列表选择一个并将其与该客户绑定。

However, after the submit, the MembershipType in Customer is set to null although MembershipTypeID is bound properly.但是,提交后,尽管MembershipTypeID绑定正确,但Customer 中的MembershipType设置为null

I have no idea how to solve this problem.我不知道如何解决这个问题。 Any advice is appreciated.任何建议表示赞赏。 Thank you in advance.先感谢您。

The code:编码:

Customer class:客户类别:

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; }
}

Customer controller:客户控制器:

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");
    }
}

NewCustomerViewModel新客户视图模型

public class NewCustomerViewModel
{
    public IEnumerable<MembershipType> MembershipTypes { get; set; }
    public Customer Customer { get; set; }
}

View for adding a new customer:添加新客户的视图:

    @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>
}  

This is normal since you did not set the MembershipType , you set the MembershipTypeId这是正常的,因为您没有设置MembershipType ,而是设置了MembershipTypeId

and when it is saved in the database the MembershipTypeId is Saved当它保存在数据库中时, MembershipTypeId被保存

Then when you query this customer via the Details Action you will get the MembershipType populated properly.然后,当您通过详细信息操作查询此客户时,您将正确填充MembershipType

Alternatively you can Populate it using the contextManager或者,您可以使用contextManager填充它

By simply通过简单地

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

but this will cost you unnecessary round to the database.但这会花费您不必要的访问数据库的费用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM