【发布时间】:2017-05-17 16:04:51
【问题描述】:
我需要将字段的值从视图传递到控制器。我的代码适用于文本框,但不适用于下拉菜单。我已经看到了很多 Html.DropdownlistFor 的答案,但对于这种情况却没有。有什么办法可以做到吗?
查看:
@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)
{
}
This answer 解决了同样的问题,但视图中没有文本框。
为了澄清,搜索部分只是我观点的一部分。我在它下面有一个分页列表。所以我需要 IsSearch 字段来查看是否按下了搜索按钮,或者它是否是页面更改,它也将转到相同的操作方法。
【问题讨论】:
标签: c# asp.net-mvc html.beginform