我有一个局部视图,显示了一个带有几个选项的搜索框
model PatientSearchViewModel
@using (Html.BeginForm("Index", "Patient", FormMethod.Post, new { @class = "form-inline" }))
{
<form class="form-inline">
<label style="padding-right:20px;">Search for a patient</label>
@Html.EditorFor(model => model.SearchText, new { htmlAttributes = new { @class = "form-control", placeholder = "Search...", autocomplete = "off" } })
<div class="checkbox">
<div class="checkbox" style="padding-left:20px; padding-right:20px;">
<label>@Html.EditorFor(model => model.ShowOnlyOpenClients) Show only open clients</label>
</div>
</div>
<button type='submit' name='seach' id='search-btn' class="btn btn-primary"><i class="fa fa-search"></i>Search</button>
</form>
}
当提交按钮被点击时,我希望它转到我的方法,搜索内容已经完成,但是它会转到HttpGet方法,该方法为PatientSearchViewModel带回一个空值。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(PatientSearchViewModel patient)
{
PatientSearchViewModel patientVM = GetSearchResults(patient);
return View(patientVM);
}
任何人都可以解释我是如何让这个工作的吗?
答案 0 :(得分:0)
我已经执行了您在此处粘贴的代码段并找到了
[ValidateAntiForgeryToken]
属性,所以你需要添加@Html.AntiForgeryToken();
这个
您partial view
内的陈述
@using(Html.BeginForm()){ }
阻止。PatientController
。供您参考(这是我的代码)
@model WebApplication.Models.PatientSearchViewModel
@using (Html.BeginForm("Index", "Patient", FormMethod.Post, new { @class = "form-inline" }))
{
@Html.AntiForgeryToken();
<label style="padding-right:20px;">Search for a patient</label>
@Html.EditorFor(model => model.SearchText, new { htmlAttributes = new { @class = "form-control", placeholder = "Search...", autocomplete = "off" } })
<div class="checkbox">
<div class="checkbox" style="padding-left:20px; padding-right:20px;">
<label>@Html.EditorFor(model => model.ShowOnlyOpenClients) Show only open clients</label>
</div>
</div>
<button type='submit' name='seach' id='search-btn' class="btn btn-primary"><i class="fa fa-search"></i>Search</button>
}
建议:因为您使用了@using(Html.BeginForm())
,它将以任何方式转换为<form>
标记,而不是在其中使用其他<form class="form-inline">
而是创建混淆请你删除它。
我希望这能解决你的问题。