2014-09-26 96 views
0

我有2頁。首先我只有表單發佈數據進行搜索,第二我也有相同的搜索和容器與搜索結果的形式。當我從第一個表單發佈 - 模型到搜索方法來了我所期望的,但從第二個表單模型屬性設置爲默認值(int - 0,布爾 - 假,字符串 - null等)發佈模型始終是空的

什麼是問題?


首先FOM:

<form class="form-horizontal" action="@Url.Action("Search", "Home")" method="POST"> 
    @Html.ValidationSummary() 
    <fieldset> 

     <!-- Form Name --> 
     <legend>Search</legend> 

     <!-- Search input--> 
     <div class="form-group"> 
      <label class="col-md-4 control-label" for="CertID">Certificate ID</label> 
      <div class="col-md-4"> 
       <input id="CertID" name="CertID" type="search" placeholder="Certificate ID" class="form-control input-md" required=""> 

      </div> 
     </div> 

     <!-- Search input--> 
     <div class="form-group"> 
      <label class="col-md-4 control-label" for="CertAuthCode">Certificate AuthCode</label> 
      <div class="col-md-4"> 
       <input id="CertAuthCode" name="CertAuthCode" type="search" placeholder="Certificate AuthCode" class="form-control input-md" required=""> 

      </div> 
     </div> 
     <div class="form-group"> 
      <div class="col-md-offset-4 col-md-4"> 
       <div class="checkbox"> 
        <label> 
         <input type="checkbox" name="IncludePreviousQuestionnaire" value="true"> Include Previous Questionnaire? 
        </label> 
       </div> 
      </div> 
     </div> 

     <!-- Button --> 
     <div class="form-group"> 
      <label class="col-md-4 control-label" for="searchCert"></label> 
      <div class="col-md-4"> 
       <button id="searchCert" type="submit" class="btn btn-primary">Search</button> 
      </div> 
     </div> 

    </fieldset> 

第二形態:進行搜索請求

<form class="form-horizontal" action="@Url.Action("Search", "Home")" method="POST"> 
@Html.ValidationSummary() 
<fieldset> 

    <!-- Form Name --> 
    <legend>Search</legend> 

    <!-- Search input--> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.SearchCertRequest.CertID, new { @class = "col-md-4 control-label" }) 
     @*<label class="col-md-4 control-label" for="CertID">Certificate ID</label>*@ 
     <div class="col-md-4"> 
      @Html.TextBoxFor(m => m.SearchCertRequest.CertID, new { @placeholder = "Certificate ID", @class = "form-control input-md", @required="required" }) 
      @*<input id="CertID" name="SearchCertRequest.CertID" type="search" placeholder="Certificate ID" class="form-control input-md" required="">*@ 
      @Html.ValidationMessageFor(m => m.SearchCertRequest.CertID) 
     </div> 
    </div> 

    <!-- Search input--> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.SearchCertRequest.CertAuthCode, new { @class = "col-md-4 control-label" }) 
     @*<label class="col-md-4 control-label" for="CertAuthCode">Certificate AuthCode</label>*@ 
     <div class="col-md-4"> 
      @Html.TextBoxFor(m => m.SearchCertRequest.CertAuthCode, new { @placeholder = "Certificate AuthCode", @class = "form-control input-md", @required = "required" }) 
      @*<input id="CertAuthCode" name="SearchCertRequest.CertAuthCode" type="search" placeholder="Certificate AuthCode" class="form-control input-md" required="">*@ 
      @Html.ValidationMessageFor(m => m.SearchCertRequest.CertAuthCode) 
     </div> 
    </div> 
    <div class="form-group"> 
     <div class="col-md-offset-4 col-md-4"> 
      <div class="checkbox"> 
       <label> 
        @Html.CheckBoxFor(m=>m.SearchCertRequest.IncludePreviousQuestionnaire) 
        @*<input type="checkbox" name="SearchCertRequest.IncludePreviousQuestionnaire">*@ Include Previous Questionnaire? 
       </label> 
      </div> 
     </div> 
    </div> 

    <!-- Button --> 
    <div class="form-group"> 
     <label class="col-md-4 control-label" for="searchCertBtn"></label> 
     <div class="col-md-4"> 
      <button id="searchCertBtn" name="searchCertBtn" type="submit" class="btn btn-primary">Search</button> 
     </div> 
    </div> 

</fieldset> 

型號:

public class SearchCertRequest 
{ 
    [Required] 
    public long CertID { get; set; } 
    [Required] 
    public string CertAuthCode { get; set; } 
    public bool IncludePreviousQuestionnaire { get; set; } 
} 
存在模型對於第二個表格中下一個:

更新

public class SearchViewModel 
    { 
     public SearchCertModel CertModel { get; set; } // Serch Result 
     public SearchCertRequest SearchCertRequest { get; set; } // Search Request 
    } 

議決:我從搜索表單局部視圖,現在它的工作。有人可以解釋爲什麼嗎?謝謝。

+0

什麼是您的第二種形式的模型? (它是一個包含屬性SearchCertRequest的類),所以你的post方法參數應該是該模型,而不是'SearchCertRequest' – 2014-09-26 10:18:18

+0

@StephenMuecke,是的。請檢查更新 – demo 2014-09-26 10:24:24

回答

0

兩種形式回發到相同的方法(Home控制器的檢索方法)。如第一個正在工作的簽名必須是

[HttpPost] 
public ActionResult (SearchCertRequest model) 
{ 

但所述第二形式是生成控制用於SearchViewModel所以簽名將必須

[HttpPost] 
public ActionResult (SearchViewModel model) 
{ 

編輯

要解釋你的EDI評論噸。用第二種形式生成控件,如<input name="SearchCertRequest.CertID" ...>。當您創建一個局部視圖並將它SearchCertRequest屬性SearchViewModel,它將生成控件,如<input name="CertID" ...>這將正確地回發到public ActionResult (SearchCertRequest model)

+0

也許我不明白你的評論。這是有道理的,謝謝。 – demo 2014-09-26 10:40:39