Asp.net MVC表格发布

时间:2016-09-10 09:58:33

标签: asp.net asp.net-mvc forms post

我在Contact_Us视图中有这样的表单

@model vidiaweb_com.Models.Contact_US
....
<div id="contactus">
   <div class="container">
    <form class="form-group col-md-8">
        <div id="contactuspost">
            <h3 class="txtformat">Contact Us</h3>
            <p class="txtformat">
                We are here to answer any questions you may have. Reach out to us and we will respond as soon as we can.
            </p>
            <p class="txtformat">
                Even if there is something you have always wanted to experience and can't find it on combadi, let us know and we promise we'll do our best to find it for you and send you there.
            </p>
            <br />
            <div class="form">
                @using (Html.BeginForm("Create", "Contact_Us"))
                {
                    @Html.AntiForgeryToken()
                    <div class="form-horizontal">
                        <div class="form-group">
                            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
                            <div class="col-md-12">
                                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                            </div>
                        </div>                            
                        ....
                    </div>
                }
            </div>
        </div>
    </form>
   </div>
</div>
....
@Html.Partial("_MainFooter")    

这是我的Contact_UsController

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,Email,Phone,Message,Date")] Contact_US contact_US)
{
    if (ModelState.IsValid)
    {
        db.Contact_US.Add(contact_US);
        db.SaveChanges();
        return RedirectToAction("Index","Home");
    }
    return RedirectToAction("Index", "Home");
}

但是当我填写表单然后点击提交按钮时,它不会在Create控制器中调用Contact_Us操作。这样的事情在我的网址

http://localhost:50074/Contact_Us/Index?__RequestVerificationToken=nrlDXOQglmGEzSQMqOqxm8ol4GiKeLffHoQUnLmuwhlIGcSFQfBrQxhZA8EL39nPLmG1FJQK42X284v60l6oepOytsmHLgwDOJYOgfmYnFU1&Name=dg&Email=d%40d.com&Phone=SF&Date=&Message=SFD

并将其重定向到我的Contact_Us索引视图。

我的项目中有另一个表单,但是这个表单正常工作。有谁知道问题会是什么?感谢

1 个答案:

答案 0 :(得分:1)

您有嵌套表单(<form class="form-group col-md-8">包含您的@Html.BeginForm()),这是无效的HTML并且不受支持。

您看到的网址是因为您的浏览器提交了最外层的表单。由于表单的默认方法是GET,并且默认操作是提交给生成它的方法(在您的情况下为Index()),因此它为每个表单控件生成一个查询字符串值。

删除<form class="form-group col-md-8">标记及其结束</form>标记,Html.BeginForm()生成的表单将提交给Create() POST方法。