2013-10-08 51 views
3

好吧,after long investigation,它似乎是當我有一個視圖創建工作與_layout.cshtml - 提交按鈕的形式我沒有' t工作(沒有行動返回給控制器)​​。MVC 4 表單提交按鈕不工作當視圖使用主佈局

僅當我創建視圖並取消選中「使用佈局或母版頁」時 - 該按鈕已工作!

這似乎非常不清楚,所以 - 我怎樣才能看到一般的_layout.cshtml和working form button

下面

: 嘗試實施MVC4(+剃刀)

控制器(應該得到這個職位,動作)的一種形式:

public class GeneralController { 
     [HttpPost] 
       public ActionResult SearchResults(SearchParamsModel searchParams) 
       { 
        // doin some stuff here 
        return View("SearchResultsView"); 
       } 
} 

視圖(.cshtml)

@model Models.SearchParamsModel 
    @using (Html.BeginForm("SearchResults", "General", FormMethod.Post)) 
      { 
<section class="form-field"> 
          <input type="text" name="Property1" id="Property1" class="field field139 autocomplete-init-no-img" /> 
          <label for="Property1">value1</label> 
       <form action="" method="post" class="clearfix">   
        <input 
         type="submit" 
         value="some value" 
         class="submit btn blue-btn special-submit" /> 
       </form> 
     </section> 
     } 

模型

public class SearchParamsModel 
    { 
     public string Property1{ get; set; } 
    } 
+2

把你的代碼看看有什麼問題 – sino

回答

11

您應該刪除你的內心形式的標籤,

@using (Html.BeginForm("SearchResults", "General", FormMethod.Post)) 

會爲你生成一個表單標籤。

此外,你應該使用HTML輔助生成表單元素:

@Html.LabelFor(model => model.Property1) 
@Html.TextBoxFor(model => model.Property1) 

可能是一個可能的模式結合的問題,由於這一點。 提交按鈕屬於您的嵌套內部表單,沒有模型在這裏提交。

@model MvcApplication2.Models.SearchParamsModel 

@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

@using (Html.BeginForm("SearchResults", "General", FormMethod.Post)) 
    <section class="form-field"> 
     @Html.LabelFor(model => model.Property1) 
     @Html.TextBoxFor(model => model.Property1) 
     <input type="submit" value="some value" class="submit btn blue-btn special-submit" /> 
    </section> 
} 
+1

謝謝!工作!所以,不需要在窗體上聲明 – user1025852

1

您在嵌套Form申報提交按鈕,輸入Property1不是形式的後代。將輸入元素移至嵌套窗體或完全刪除嵌套窗體

編輯:您的嵌套窗體元素也未指定操作,因此如果General/SearchResults不是此視圖的默認窗口,則不會得到您的預期結果

2

我有類似的問題,但不是你上述同樣的問題,解決了這種方式:

我的「MVC 4佈局頁面」有「形」佈局的文檔裏面的標籤。另一方面,我的視圖連接到佈局頁面(查看頁面也有「使用(Html.BeginForm()){...}」)

因此,從佈局頁面中刪除「表單」標籤應該做的事情,「因爲從查看頁面使用(Html.BeginForm()){...}」)「不再嵌套