我有2页。首先我只有表格来发布搜索数据,第二个我也有相同的搜索形式和带搜索结果的容器。当我从第一个表单发布时 - 模型到搜索方法就像我预期的那样,但是从第二个表单模型属性被设置为默认值(int - 0,bool - false,string - null等)
有什么问题?
First 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 :(得分:0)
两种形式都回发到同一种方法(家庭控制器的搜索方法)。首先是工作签名必须是
[HttpPost]
public ActionResult (SearchCertRequest model)
{
但第二种形式是为SearchViewModel
生成控件,因此签名必须是
[HttpPost]
public ActionResult (SearchViewModel model)
{
修改强>
在编辑中解释评论。第二种形式是您的生成控件,例如<input name="SearchCertRequest.CertID" ...>
。当您创建部分视图并将SearchCertRequest
属性SearchViewModel
传递给它时,它会生成<input name="CertID" ...>
等控件,这些控件将正确地回发到public ActionResult (SearchCertRequest model)