2016-09-10 124 views
0

我有形式這樣在我CONTACT_US視圖Asp.net MVC形式發佈

@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&Name=dg&Email=d%40d.com&Phone=SF&Date=&Message=SFD

並再次將其重定向到我CONTACT_US索引視圖。

我有另一種形式在我的項目是這樣,但一個正常工作。有沒有人有任何想法可能會是什麼問題?謝謝

+1

的是,在您的視圖的唯一形式? (它看起來像你可能有嵌套的表單是無效的)。它是產生這個視圖的'Index()'方法。 –

+0

是@StephenMuecke在我看來,我只有這一種形式。感謝您的關心 –

+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方法的形式。

+0

非常感謝。解決了 –