创建按钮单击功能在mvc3中不起作用?

时间:2012-07-13 14:21:22

标签: asp.net-mvc-3 c#-4.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页面中的创建按钮时,它应该在我的控制器中创建方法.....我在这里做错了什么.........

3 个答案:

答案 0 :(得分:1)

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

        <% using (Html.BeginForm()) { %>
      <form action=”/members/Create” method=”post”></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);
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)