0

大家好,我有一个表格,我有一个添加新项目链接...当我点击它时,它会将我带到一个创建页面,其中我有用于插入新记录的文本框和一个创建按钮..但我点击创建按钮它不工作任何人都可以帮助我我在哪里做错了这是我的代码

这是我的 create.aspx 页面:

             <%: ViewBag.Title="Create" %>

         <fieldset>
       <legend>Projects</legend>

  <div class="editor-label">
        <%: 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">
       <%: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">
    <%:Html.LabelFor(model=>model.status) %>
    </div>
    <div class="editor-field">
    <%:Html.EditorFor(model=>model.status) %>
    <%:Html.ValidationMessageFor(model=>model.status) %>
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
  </fieldset>

这是我的控制器功能:

              public ActionResult Create()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Create(ProjectModel model)
    {
        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 = model.status;
            insertcommande.ExecuteNonQuery();
        }
        return View( modelList);
    }

当我单击aspx页面中的创建按钮时,它应该在我的控制器中创建方法.....我在这里做错了什么............

4

3 回答 3

1

我把它放在我的 create.aspx 页面 BeginForm 语句中:

        <% using (Html.BeginForm()) { %>
      <form action=”/members/Create” method=”post”&gt;</form>

并添加

[AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Create()
{
    return View();
}
[AcceptVerbs(HttpVerbs.post)]
public ActionResult Create(ProjectModel model)
{
    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 = model.status;
        insertcommande.ExecuteNonQuery();
    }
    return View( modelList);
}
于 2012-07-16T07:03:31.020 回答
0

您的视图中没有form。这是一个问题。

于 2012-07-13T14:30:20.447 回答
0

访问 MSDN 获取 BeginForm:http: //msdn.microsoft.com/en-us/library/dd410596.aspx

于 2012-07-14T21:10:53.827 回答