将下拉选择的值传递给 Html.BeginForm() 中的视图
Pass the dropdown selected value to the view in Html.BeginForm()
我需要将字段的值从视图传递到控制器。我的代码适用于文本框,但不适用于下拉列表。我已经看到很多 Html.DropdownlistFor 的答案,但 none 对于这种情况。我有办法做到这一点吗?
查看:
@var searchTypes = new SelectList(new[]
{
new SelectListItem {Value = "0", Text = "Email"},
new SelectListItem {Value = "1", Text = "Last Name"},
new SelectListItem {Value = "2", Text = "First Name"},
}, "Value", "Text");
@using (Html.BeginForm("Index", "Users", new { isSearch = 1, searchTerm = "SearchTerm", searchType="ddlSearchType" }, FormMethod.Post))
{
<div class="well well-sm search">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
@Html.TextBox("SearchTerm",(string)ViewBag.SearchTerm, new {name= "SearchTerm", @class = "form-control", placeholder = "Search Term" })
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<div class="controls">
@Html.DropDownList("ddlSearchType", searchTypes, new { id = "searchtypeselect", @class = "form-control" })
</div>
</div>
</div>
<div class="col-sm-2">
<button class="btn btn-primary pull-right" type="submit"><i class="glyphicon glyphicon-search"></i>Search</button>
</div>
</div>
</div>
}
控制器:
public ActionResult Index(int? isSearch,string searchTerm,string searchType)
{
}
解决了同样的问题,但它在视图中没有文本框。
澄清一下,搜索部分只是我观点的一部分。我在它下面有一个分页列表。所以我需要 IsSearch 字段来查看是否按下了搜索按钮,或者它是否是页面更改,这也将转到相同的操作方法。
要更正这些值的问题,您需要向搜索列表控制器添加第四个参数,该参数应该是所选值。
@var searchTypes = new SelectList(new[]
{
new SelectListItem {Value = "0", Text = "Email"},
new SelectListItem {Value = "1", Text = "Last Name"},
new SelectListItem {Value = "2", Text = "First Name"},
}, "Value", "Text", selectedValueHere);
接下来,您应该更改 BeginForm 帮助器并删除那里的路由属性。您可能没有为这些定义路由,并且您希望值来自表单提交,而不是您在帮助程序中发送的硬编码字符串。您还可以删除该方法,因为它默认为 POST.
@using (Html.BeginForm("Index", "Users",new { isSearch = 1 }))
最后,您需要调整下拉菜单的名称,使其与 POST 操作中预期的名称正确匹配。
@Html.DropDownList("SearchType", searchTypes, new { id = "searchtypeselect", @class = "form-control" })
答案就在您发布的 link 中。您需要从 Html.BeginForm 中删除下拉列表参数,并在控制器和下拉列表名称中使用相同的字符串。
@using (Html.BeginForm("Index", "Users", new { isSearch = 1, searchTerm = "SearchTerm"}, FormMethod.Post))
public ActionResult Index(int? isSearch,string searchTerm,string ddlSearchType)
{
}
我需要将字段的值从视图传递到控制器。我的代码适用于文本框,但不适用于下拉列表。我已经看到很多 Html.DropdownlistFor 的答案,但 none 对于这种情况。我有办法做到这一点吗?
查看:
@var searchTypes = new SelectList(new[]
{
new SelectListItem {Value = "0", Text = "Email"},
new SelectListItem {Value = "1", Text = "Last Name"},
new SelectListItem {Value = "2", Text = "First Name"},
}, "Value", "Text");
@using (Html.BeginForm("Index", "Users", new { isSearch = 1, searchTerm = "SearchTerm", searchType="ddlSearchType" }, FormMethod.Post))
{
<div class="well well-sm search">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
@Html.TextBox("SearchTerm",(string)ViewBag.SearchTerm, new {name= "SearchTerm", @class = "form-control", placeholder = "Search Term" })
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<div class="controls">
@Html.DropDownList("ddlSearchType", searchTypes, new { id = "searchtypeselect", @class = "form-control" })
</div>
</div>
</div>
<div class="col-sm-2">
<button class="btn btn-primary pull-right" type="submit"><i class="glyphicon glyphicon-search"></i>Search</button>
</div>
</div>
</div>
}
控制器:
public ActionResult Index(int? isSearch,string searchTerm,string searchType)
{
}
澄清一下,搜索部分只是我观点的一部分。我在它下面有一个分页列表。所以我需要 IsSearch 字段来查看是否按下了搜索按钮,或者它是否是页面更改,这也将转到相同的操作方法。
要更正这些值的问题,您需要向搜索列表控制器添加第四个参数,该参数应该是所选值。
@var searchTypes = new SelectList(new[]
{
new SelectListItem {Value = "0", Text = "Email"},
new SelectListItem {Value = "1", Text = "Last Name"},
new SelectListItem {Value = "2", Text = "First Name"},
}, "Value", "Text", selectedValueHere);
接下来,您应该更改 BeginForm 帮助器并删除那里的路由属性。您可能没有为这些定义路由,并且您希望值来自表单提交,而不是您在帮助程序中发送的硬编码字符串。您还可以删除该方法,因为它默认为 POST.
@using (Html.BeginForm("Index", "Users",new { isSearch = 1 }))
最后,您需要调整下拉菜单的名称,使其与 POST 操作中预期的名称正确匹配。
@Html.DropDownList("SearchType", searchTypes, new { id = "searchtypeselect", @class = "form-control" })
答案就在您发布的 link 中。您需要从 Html.BeginForm 中删除下拉列表参数,并在控制器和下拉列表名称中使用相同的字符串。
@using (Html.BeginForm("Index", "Users", new { isSearch = 1, searchTerm = "SearchTerm"}, FormMethod.Post))
public ActionResult Index(int? isSearch,string searchTerm,string ddlSearchType)
{
}