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

私の問題は、送信ボタンをクリックしても新規作成ページで検証が開始されないことですが、編集ページで cilck する前に検証が行われます。

または、検証を提供する他の方法があります

4

1 に答える 1

0

実際には、検証が失敗したかどうかを確認していません。投稿するコントローラーメソッドでは、チェックが必要です:

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

    return RedirectToAction("WhereYouWantToGoAfterwards");
}

return View(model);

編集:

上で述べたように、コードのエラーを実際にチェックしているわけではありません。ただし、Create と Edit で異なる効果として、Edit では View() が返されますが、Create では RedirectToAction が返されます。Redirect は ModelState をクリアするので、エラーは表示されません。

データに基づいて作業を開始する前に、ModelState.IsValid を確認する必要があります。そうしないと、多くのトラブルに遭遇することになります。

于 2012-07-28T04:23:09.170 に答える