簡體   English   中英

Asp.net MVC 發布表單空值

[英]Asp.net MVC post form null value

我目前正在學習 Mosh 的 asp.net mvc 課程。

最近開始用form來發數據,但是遇到了無法解決的問題。

因此,用於添加 Customer 的表單使用包含可用MembershipTypes NewCustomerViewModel 然后,在表單中,我使用下拉列表選擇一個並將其與該客戶綁定。

但是,提交后,盡管MembershipTypeID綁定正確,但Customer 中的MembershipType設置為null

我不知道如何解決這個問題。 任何建議表示贊賞。 先感謝您。

編碼:

客戶類別:

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

添加新客戶的視圖:

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

這是正常的,因為您沒有設置MembershipType ,而是設置了MembershipTypeId

當它保存在數據庫中時, MembershipTypeId被保存

然后,當您通過詳細信息操作查詢此客戶時,您將正確填充MembershipType

或者,您可以使用contextManager填充它

通過簡單地

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

但這會花費您不必要的訪問數據庫的費用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM