我有一点问题,我必须将参数从视图传递给控制器,我已经成功使用输入字段,但使用@html.dropdownList我不知道该怎么做,能帮到我吗,这是我的代码:
观点:
@using (Ajax.BeginForm("GetFilterList", "Inconsistencies",
new AjaxOptions
{
HttpMethod = "POST",
UpdateTargetId = "refreshList",
}, new { @id = "FileList" }
))
{
<span class="input-group-addon">Date de début</span>
<input type="text" class="form-control date-picker" placeholder="jj/mm/aaaa" id="StartDate" name="StartDate" />
<span class="input-group-addon">Date de fin</span>
<input type="text" class="form-control date-picker" placeholder="jj/mm/aaaa" id="EnDate" name="EnDate" />
<span class="input-group-addon">Etat inco</span>
@Html.DropDownListFor(m => m.Inconsistences.InconsistencyStateId, Model.StateList, new { id = "state", }) //how to pass the selected value to controller ?
<span class="input-group-addon">Type inco</span>
<input type="text" class="form-control" id="type" name="type" />
<button type="submit" class="btn btn-sm fsc-btn-1" id="search">
<i class="fa fa-search"></i>
<span>Recherche</span>
</button>
}
控制器:
[HttpPost]
public ActionResult GetFilterList(DateTime? StartDate, DateTime? EnDate, decimal? State, decimal? type)
{
InconsistenciesModel model = new InconsistenciesModel();
model.FillDDL();
model.GetListFilter(StartDate, EnDate,State, type);
return PartialView("_InconsistenciesList", model);
}
答案 0 :(得分:1)
将Ajax.BeginForm简化为
@using (Ajax.BeginForm("GetFilterList", "Inconsistencies")
{
.......
}
此搜索按钮的操作会将所有表单值(包括您的下拉列表的选定值)发布到控制器。
希望这有帮助。
答案 1 :(得分:1)
它可以帮助您了解任何值如何传递给控制器。
<form>
<input name="someValue" type="text" value="test" />
<button type="submit">Submit</submit>
</form>
使用标准 application / x-www-form-urlencoded 方案,将someValue=test
的值发送到服务器。
你可以直接使用ASP.net中的标准Request api访问该值。
public ActionResult Test(){
string someValue = Request.Form("someValue");
}
MVC框架通过自动绑定Form和Querystring以及将参数值路由到MVC操作参数来添加一些不错的功能。
public ActionResult Test(string someValue){
}
框架还具有绑定到复杂类型的功能(https://msdn.microsoft.com/en-us/magazine/hh781022.aspx)。
在您的情况下,m=> m.Inconsistences.InconsistencyStateId
最有可能被翻译成名称“Inconsistences_InconsistencyStateId”
public ActionResult GetFilterList(string Inconsistences_InconsistencyStateId){
}
or
// MVC will try to convert the value into an integer
public ActionResult GetFilterList(int Inconsistences_InconsistencyStateId){
}
// MVC will throw an error if the parameter is missing, so you may need to make any ValueType parameters nullable.
public ActionResult GetFilterList(int? Inconsistences_InconsistencyStateId){
}
或者你可以自动模型绑定
public class Inconsistences {
public int InconsistencyStateId {get;set;}
}
public class InconsistenciesModel {
public Inconsistences Inconsistences {get;set;}
}
public ActionResult GetFilterList(InconsistenciesModel model) {
int id = model.Inconsistences.InconsistencyStateId;
}
答案 2 :(得分:1)
请将您的下拉列表更改为
@Html.DropDownListFor(m => m.Inconsistences.InconsistencyStateId, Model.StateList, new { name = "state", })
OR
如果m=> m.Inconsistences.InconsistencyStateId
主要被渲染到名称属性&#34; Inconsistences_InconsistencyStateId
&#34;,则使用如下
在控制器中。或者请检查渲染的html中的下拉名称属性并相应地更改操作参数。
public ActionResult GetFilterList(DateTime? StartDate, DateTime? EnDate, decimal? State, int Inconsistences_InconsistencyStateId)
{
...
}
这会奏效。因为表单帖子基于表单中的名称值对。