2011-04-12 5 views
1
Here is my controller codes 

// 
     // GET: /Department/Create 

     public ActionResult Create() 
     { 
      return View(new department()); 
     } 

     // 
     // POST: /Department/Create 

     [HttpPost] 
     public ActionResult Create(department dept) 
     { 
      try 
      { 
       dbContext.AddTodepartments(dept); 
       dbContext.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(); 
      } 
     } 

     // 
     // GET: /Department/Edit/5 

     public ActionResult Edit(int id) 
     { 
      return View(dbContext.departments.Single(d => d.DeptID == id)); 
     } 

     // 
     // POST: /Department/Edit/5 

     [HttpPost] 
     public ActionResult Edit(int id, FormCollection collection) 
     { 
      var dept = dbContext.departments.Single(d => d.DeptID == id); 
      try 
      { 
       UpdateModel(dept); 
       dbContext.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(dept); 
      } 
     } 

Here is the partial class 

    [MetadataType(typeof(DepartmentMetaData))] 
    public partial class department 
    { 
    } 
    public class DepartmentMetaData 
    { 
     [Required(AllowEmptyStrings = false, ErrorMessage = "Department name required.")] 
     public string DeptName { get; set; } 
    } 


    The required field validation is happening on the editing only. I can insert a null 'Department name' but on eding the values, it is not allowing to enter a null value. 

답변

1

레코드를 만들 때 ModelState.IsValid을 확인해야합니다. ModelState에 대한 자세한 정보 here

+0

감사합니다. 그게 내 문제를 해결해 줬어. – Mukesh

관련 문제