【问题标题】:Asp.net MVC Form postingAsp.net MVC 表单发布
【发布时间】:2017-01-18 09:23:43
【问题描述】:

我的 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");
}

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

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

它会再次重定向到我的 Contact_Us 索引视图中。

我的项目中有另一种形式,但它可以正常工作。有谁知道问题可能是什么?谢谢

【问题讨论】:

  • 这是您认为的唯一形式吗? (看起来您可能有无效的嵌套表单)。是Index() 方法生成了这个视图吗?
  • 是的@StephenMuecke 我认为只有这一种形式。感谢您的关心
  • 你确定吗?你能发布整个视图吗?
  • 是的,请稍等编辑@StephenMuecke
  • 您确实有嵌套表单 :) - 删除外部 &lt;form class="form-group col-md-8"&gt;

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


【解决方案1】:

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

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

移除&lt;form class="form-group col-md-8"&gt;标签及其结束&lt;/form&gt;标签,Html.BeginForm()生成的表单将提交给Create() POST方法。

【讨论】:

    猜你喜欢
    • 2023-04-09
    • 2011-08-21
    • 2011-01-15
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    相关资源
    最近更新 更多