这就是我的模型的外观
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>
<%} %>
我的问题是没有触发验证在我单击“提交”按钮时创建新页面 但是在我的编辑页面中,即使我知道,也可以通过任何人告诉我我在哪里做错了验证
或者还有其他方法可以提供验证
答案 0 :(得分:0)
您实际上并未检查是否有任何验证失败。在您发布的控制器方法中,您需要检查:
if (ModelState.IsValid) {
// do your work
return RedirectToAction("WhereYouWantToGoAfterwards");
}
return View(model);
编辑:
如上所述,您实际上并未检查代码中的错误。但是,您在Create和Edit之间看到的效果是不同的,在Edit中,您返回一个View(),但是在Create you RedirectToAction中。重定向清除了ModelState,因此您不会看到任何错误。
在开始根据数据进行工作之前,您仍需要检查ModelState.IsValid。否则,你会遇到很多麻烦。