2017-04-06 78 views
0

我是MVC的新手。我使用DropDownListFor來顯示公司名稱列表,並根據所作的選擇填寫其他客戶字段。客戶字段填充良好,但是當我嘗試發佈記錄時,即使在DropDownListFor中選擇了公司名稱,我也會收到驗證錯誤「需要公司名稱」。這裏是視圖模型:DropDownListFor選擇不被保存

 namespace CMSUsersAndRoles.Models 
{ 
    public class QuoteViewModel 
    { [Key] 
     [Display(Name = "Quote Id")] 
     public int QuoteId { get; set; } 
     // Columns from Customer table 
     [Required(ErrorMessage = "Please select a company")] 
     [Display(Name = "Customer Id")] 
     public int? CustomerId { get; set; } 
     [Display(Name = "Sales Rep")] 
     public string SalesRep { get; set; } 
     [Display(Name = "First Name")] 
     public string FirstName { get; set; } 
     [Display(Name = "Last Name")] 
     public string LastName { get; set; } 
     [Required] 
     public string Company { get; set; } 
     [Display(Name = "Address 1")] 
     public string Address1 { get; set; } 
     [Display(Name = "Address 2")] 
     public string Address2 { get; set; } 
     public string City { get; set; } 
     public string State { get; set; } 
     [StringLength(10)] 
     [Display(Name = "Zip Code")] 
     [RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid Zip Code")] 
     public string PostalCode { get; set; } 
     [Phone] 
     [Display(Name = "Work Phone")] 
     public string WorkPhone { get; set; } 
     [Phone] 
     [Display(Name = "Cell Phone")] 
     public string CellPhone { get; set; } 
     [EmailAddress] 
     public string Email { get; set; } 
     [Range(0, 100)] 
     public decimal? Discount { get; set; } 
     [Display(Name = "Payment Terms")] 
     public int? PaymentTerms { get; set; } 
     // Columns from QuoteDetail table 
     [Display(Name = "Quote Detail")] 
     public List<QuoteDetail> QuoteDetail { get; set; } 
     // Columns from Quote table 


     public decimal Subtotal { get; set; } 
     public decimal Tax { get; set; } 
     public decimal Total { get; set; } 
     [DataType(DataType.Date)] 
     [Display(Name = "Quote Date")] 
     public DateTime? QuoteDate { get; set; } 
     [DataType(DataType.Date)] 
     [Display(Name = "Good Until")] 
     public DateTime? GoodUntil { get; set; } 
     [DataType(DataType.Date)] 
     [Display(Name = "Quote Sent")] 
     public DateTime? QuoteSent { get; set; } 
     [DataType(DataType.Date)] 
     [Display(Name = "Date Approved")] 
     public DateTime? DateApproved { get; set; } 
     [DataType(DataType.Date)] 
     [Display(Name = "Date Ordered")] 
     public DateTime? DateOrdered { get; set; } 

    } 
} 

下面是從視圖代碼:

@Html.DropDownListFor(model => model.CustomerId, new SelectList(ViewBag.Customers, "CustomerId", "Company"), "---Select one---", new { htmlAttributes = new { @class = "company" } }); 
    @Html.HiddenFor(model => model.Company) 
    @Html.ValidationMessageFor(model => model.Company, "", new { @class = "text-danger" }) 

這裏是獲取:

public ActionResult Create() 
     { 
      QuoteViewModel qvm = new QuoteViewModel(); 

      var customers = db.Customers.ToList(); 
      ViewBag.Customers = customers; 

      return View(qvm); 
     } 

這裏是爲POST代碼:

[HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Create(QuoteViewModel qvm) 
{ 

if (ModelState.IsValid) 
      { 
       Quote quote1 = new Quote(); 

       quote1.CustomerId = qvm.CustomerId; 
       ... 
       customer.CustomerId = (int)qvm.CustomerId; 
       ... 
       customer.Company = qvm.Company;  

       db.Entry(customer).State = EntityState.Modified; 
       try 
        { 
         db.SaveChanges(); 
        } 
        catch (DbUpdateConcurrencyException ex) 
        { 

         var objContext = ((IObjectContextAdapter)db).ObjectContext; 
         // Get failed entry 
         var entry = ex.Entries.Single(); 
         // Now call refresh on ObjectContext 
         objContext.Refresh(RefreshMode.ClientWins, entry.Entity); 


        } 


       return RedirectToAction("Index"); 
      } 

      var customers = db.Customers.ToList(); 
      ViewBag.Customers = customers; 

      return View(qvm); 
     } 

我錯過了什麼?任何幫助都感激不盡。

+0

公司名稱未被選中 - 所有被選中的值都是CustomerId的值(這是您的下拉列表綁定的值)。您沒有向我們展示相關的代碼。 –

+0

而你的'customer.Company = qvm.Company;'在POST方法中的代碼行表明你的視圖模型有一個'Company Company'屬性 - 視圖模型不包含數據模型的屬性。 –

+0

你需要看什麼代碼? –

回答

0

我沒有在下面設置HiddenFor的值。謝謝@StephenMuecke。

@Html.HiddenFor(model => model.Company)