2012-07-28 100 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。否则,你会遇到很多麻烦。