0

这就是我的模型的外观

      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>
 <%} %>

我的问题是当我单击提交按钮时,在我的创建新页面中没有触发验证,但是在我在编辑页面中单击之前,验证就已经开始了,谁能告诉我我在哪里做错了

或者有任何其他方法可以提供验证

4

1 回答 1

0

您实际上并没有检查是否有任何验证失败。在您发布的控制器方法中,您需要检查:

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

    return RedirectToAction("WhereYouWantToGoAfterwards");
}

return View(model);

编辑:

正如我上面所说,您实际上并没有检查代码中的错误。但是,您看到的 Create 和 Edit 之间不同的效果是,在 Edit 中,您返回一个 View(),但在 Create 中您是 RedirectToAction。重定向会清除 ModelState,因此您不会看到任何错误。

在开始根据数据进行工作之前,您仍然需要检查 ModelState.IsValid 是否存在。否则,你会遇到很多麻烦。

于 2012-07-28T04:23:09.170 回答