2016-04-05 15 views
0

Bu, Veri Ek Açıklamaları'nı kullanmaya çalıştığımda ilk defa kullanıyorum ve doğrulamayı sınamaya çalışırken yukarıdaki hatayı almaya devam ediyorum.MVC4 Veri Ek Açıklamaları Çalışma zamanı bağlama null başvuruda gerçekleştirilemiyor

Benim modelim benziyor: Bence böyle bakarken

public class SupplierValidationModel 
{ 
    [Display(Name = "Company Name:")] 
    [Required(ErrorMessage = "Company Name is Required")] 
    public string Name { get; set; } 

    [Display(Name = "Company Address:")] 
    [Required(ErrorMessage = "Company Address is Required")] 
    public string Address { get; set; } 

    [Display(Name = "Company City:")] 
    [Required(ErrorMessage = "Company City is Required")] 
    public string City { get; set; } 

    [Display(Name = "Company State:")] 
    [Required(ErrorMessage = "Company State is Required")] 
    public string State { get; set; } 

    [Display(Name = "Company Zip:")] 
    [Required(ErrorMessage = "Company Zip is Required")] 
    public string Zip { get; set; } 

    [Display(Name = "Company Phone:")] 
    [Required(ErrorMessage = "Company Phone is Required")] 
    [RegularExpression(@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}", ErrorMessage = "A Valid Phone Number is Required")] 
    public string Phone { get; set; } 

    [Display(Name = "Contact Name:")] 
    [Required(ErrorMessage = "Contact Name is Required")] 
    public string ContactName { get; set; } 

    [Display(Name = "Contact E-Mail:")] 
    [Required] 
    [EmailAddress(ErrorMessage = "Valid E-Mail Required")] 
    public string ContactEmail { get; set; } 

    [Display(Name = "Contact Phone:")] 
    [Required] 
    [RegularExpression(@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}", ErrorMessage = "A Valid Phone Number is Required")] 
    public string ContactPhone { get; set; } 

    [Display(Name = "Added By:")] 
    [Required(ErrorMessage = "Added By Must Be Entered")] 
    public string AddedBy { get; set; } 
} 

;

@model SupplierValidationModel 
@using (Html.BeginForm("InsertSupplier", "Admin", FormMethod.Post)) 
    { 
     @Html.ValidationSummary(false) 
     <table> 
      <tr> 
       <td>@Html.LabelFor(m => m.Name)</td> 
       <td>@Html.TextBoxFor(m => m.Name) @Html.ValidationMessageFor(m=>m.Name)</td> 
       <td>@Html.LabelFor(m => m.Address)</td> 
       <td>@Html.TextBoxFor(m => m.Address) @Html.ValidationMessageFor(m => m.Address)</td> 
      </tr> 
      <tr> 
       <td>@Html.LabelFor(m => m.City)</td> 
       <td>@Html.TextBoxFor(m => m.City) @Html.ValidationMessageFor(m => m.City)</td> 
       <td>@Html.LabelFor(m => m.State)</td> 
       <td>@Html.DropDownListFor(m => m.State, StateList.States) @Html.ValidationMessageFor(m => m.State)</td> 
      </tr> 
      <tr> 
       <td>@Html.LabelFor(m => m.Zip)</td> 
       <td>@Html.TextBoxFor(m => m.Zip) @Html.ValidationMessageFor(m => m.Zip)</td> 
       <td>@Html.LabelFor(m => m.Phone)</td> 
       <td>@Html.TextBoxFor(m => m.Phone) @Html.ValidationMessageFor(m => m.Phone)</td> 
      </tr> 
      <tr> 
       <td>@Html.LabelFor(m => m.ContactName)</td> 
       <td>@Html.TextBoxFor(m => m.ContactName) @Html.ValidationMessageFor(m => m.ContactName)</td> 
       <td>@Html.LabelFor(m => m.ContactEmail)</td> 
       <td>@Html.TextBoxFor(m => m.ContactEmail) @Html.ValidationMessageFor(m => m.ContactEmail)</td> 
      </tr> 
      <tr> 
       <td>@Html.LabelFor(m => m.ContactPhone)</td> 
       <td>@Html.TextBoxFor(m => m.ContactPhone) @Html.ValidationMessageFor(m => m.ContactPhone)</td> 
       <td>@Html.LabelFor(m => m.AddedBy)</td> 
       <td>@Html.TextBoxFor(m => m.AddedBy) @Html.ValidationMessageFor(m => m.AddedBy)</td> 
      </tr> 
      <tr> 
       <td colspan="4" align="right"><input type="submit" value="Add Supplier" /></td> 
      </tr> 
     </table> 
    <input type="hidden" name="userId" value="@ViewBag.Employee.AccountName" /> 
    } 

Son olarak benim Eylemim;

 [HttpPost] 
    public ActionResult InsertSupplier(SupplierValidationModel model) 
    { 
     // string userName = coll["userId"].ToString();    
     if (ModelState.IsValid) 
     { 
      DataOps.InsertSupplier(model); 
      return View(); 

     }else{ 
      ViewData["Valid"] = false; 
      //return RedirectToAction("SupplierInfo", "Admin", new {employeeAcct = userName }); 
      return View(model); 

     } 
    } 

olursa olsun Görünüm döndüğünüzde() tekrar sadece geri yukarı ilişkili hata mesajları ile formunu yüklemek istediğiniz ne ben çalışma zamanı hatası gerçekleştiremiyor olsun.

Çok sayıda öğreticiden geçtim ve her şey geçerli olmayacak şekilde ModelState'de doğru şekilde yükleniyor, ancak hiçbir zaman formu oluşturmuyor.

cevap

1

Muhtemelen @ ViewBag. Gizli alanınızda sahip olduğunuz bir sayfa boştur. Bunu GET eylemine yüklediyseniz, POST'a da yüklemeniz gerekir.

+0

Haklısınız! Bu tamamen benim hatam, yeniden yüklemede ViewBag değerlerini geri almıyor. teşekkür ederim –