2012-07-28 99 views
0

這是我的模型看起來如何驗證不適用於MVC3?

 public class ProjectModel 
     { 
     public static List<ProjectModel> GetList { get; set; } 
     public Int16 ID { get; set; } 
     [Required(ErrorMessage = "ProjectName is required")] 
     public string projectName { get; set; } 
     [Required(ErrorMessage = "Description is required")] 
     public string Description { get; set; } 
     [Required(ErrorMessage = "Status is required")] 
     public string status { get; set; } 
     } 

這是控制器的外觀

  #region Insert New Project 
    // 
    //View for adding new Projects/ 
    // 
    [AcceptVerbs(HttpVerbs.Get)] 
    public ActionResult Create() 
    { 
     ViewBag.Status = new SelectList(GetStatus(), "Status", "Status"); 
     return View(); 
    } 

    [AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult Create(ProjectModel model,string Status) 
    { 
     // var modelList = new List<ProjectModel>(); 
     using (SqlConnection conn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True")) 
     { 
      conn.Open(); 
      SqlCommand insertcommande = new SqlCommand("Sp_AddNewProject", conn); 
      insertcommande.CommandType = CommandType.StoredProcedure; 
      insertcommande.Parameters.Add("@ProjectName", SqlDbType.VarChar).Value = model.projectName; 
      insertcommande.Parameters.Add("@Description", SqlDbType.VarChar).Value = model.Description; 
      insertcommande.Parameters.Add("@Status", SqlDbType.VarChar).Value =Status; 
      insertcommande.ExecuteNonQuery(); 
     } 
     return RedirectToAction("Create"); 
    } 


     #region To Edit th Existing Project Record 
    // 
    //View For displaying the record to be edited/ 
    // 
    [AcceptVerbs(HttpVerbs.Get)] 
    public ActionResult Edit(int id, ProjectModel updatemodel) 
    { 
     ViewBag.Status = new SelectList(GetStatus(), "Status", "Status"); 
     SqlConnection cn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True"); 
     SqlCommand cmd = new SqlCommand("Select ProjectId,projectName,Description,status From Projects Where ProjectId=" + id, cn); 
     cn.Open(); 
     SqlDataReader dr = cmd.ExecuteReader(); 
     if (dr.Read()) 
     { 
      //if (dr[0]) != DBNull.Value) 
      updatemodel.ID = Convert.ToInt16(dr["ProjectId"]); 
      updatemodel.projectName = dr["projectName"].ToString(); 
      updatemodel.Description = dr["Description"].ToString(); 
      updatemodel.status = dr["status"].ToString(); 
     } 
     else 
     { 
      dr.Close(); 
     } 
     dr.Close(); 
     cn.Close(); 
     return View(updatemodel); 
    } 
    // 
    //Action for editing the record which is in view/ 
    // 
    [AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult Edit(ProjectModel updatemodel, FormCollection form, int id,string Status) 
    { 

     SqlConnection cn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True"); 
     SqlCommand cmd = new SqlCommand("UpdateProject", cn); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cn.Open(); 
     cmd.Parameters.Add("ProjectId", SqlDbType.Int).Value = updatemodel.ID; 
     cmd.Parameters.Add("projectName", SqlDbType.VarChar).Value = updatemodel.projectName; 
     cmd.Parameters.Add("Description", SqlDbType.VarChar).Value = updatemodel.Description; 
     cmd.Parameters.Add("status", SqlDbType.VarChar).Value = Status; 
     cn.Close(); 
     return View(updatemodel); 
    } 

    #endregion 

,這是我的aspx頁面的外觀

 <% using (Html.BeginForm()) 
    { %> 
    <%-- <form action="Create.aspx" method="post"></form>--%> 
    <%:Html.ValidationSummary(true)%> 
    <fieldset> 
    <legend style="color:Orange; font-weight:bolder;">AddNew Project</legend> 

    <div class="editor-label" style="color:Orange; font-weight:bolder;"> 
     <%: Html.LabelFor(model => model.projectName)%> 
    </div> 
    <div class="editor-field"> 
     <%:Html.EditorFor(model => model.projectName)%> 
     <%: Html.ValidationMessageFor(model => model.projectName)%> 
    </div> 

    <div class="editor-label" style="color:Orange; font-weight:bolder;"> 
     <%:Html.LabelFor(model => model.Description)%> 
    </div> 
    <div class="editor-field"> 
     <%:Html.EditorFor(model => model.Description)%> 
     <%:Html.ValidationMessageFor(model => model.Description)%> 
    </div> 
    <div class="editor-label" style="color:Orange; font-weight:bolder;"> 
    <%:Html.LabelFor(model => model.status)%> 
    </div> 
    <div class="editor-field"> 
    <%:Html.DropDownList("Status")%> 
    <%:Html.ValidationMessageFor(model => model.status)%> 
    </div> 
    <p> 
     <input type="submit" value="Create" style="color:Orange; font-weight:bolder;"/> 
     </p> 
    </fieldset> 
<%} %> 

我的問題是驗證,不會引發在我創建新的頁面,當我點擊提交按鈕 但驗證geting之前即使我cilck在我的編輯頁面可以任何人告訴我whe再我做錯了

或有任何其他的方法來提供驗證

+0

你的評論沒有意義。你說驗證沒有被解僱,那麼你說驗證發生在頁面加載之前?請清楚地問你的問題。 – 2012-07-28 04:20:51

+0

@MystereMan驗證沒有在我的創建頁面中觸發,但在我的編輯頁面驗證是在我的頁面加載時觸發的..我提到這 – SoftwareNerd 2012-07-28 04:30:14

+0

什麼是您在問題中添加的視圖名稱? – Yasser 2012-07-28 06:28:34

回答

0

你實際上並沒有檢查,看看是否有任何驗證失敗。在您發佈的控制器方法,你需要有一個檢查:

if (ModelState.IsValid) { 
    // do your work 

    return RedirectToAction("WhereYouWantToGoAfterwards"); 
} 

return View(model); 

編輯:

正如我前面所說,您實際上並不檢查代碼中的錯誤。但是,您看到的創建和編輯效果不同的是,在編輯中,您返回一個View(),但在Create you RedirectToAction中。重定向清除了ModelState,所以你不會看到任何錯誤。

在開始基於數據工作之前,您仍然需要檢查ModelState.IsValid。否則,你會遇到很多麻煩。