如何将选定值从下拉列表中从视图传递到控制器 [英] How do I pass the selected value from a dropdownlist from view to the controller

查看:118
本文介绍了如何将选定值从下拉列表中从视图传递到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





在我的视图中有一个下拉列表,其他一些控件和一个用于保存数据的输入按钮。

开保存我有ActionReult Save()

需要我在下拉列表中选择的值。

如何在控制器上获取下拉列表的选定值Actionresult Save()



浏览:

------

  var  docTypes = Model.DocumentTypes.Select(x = >   new  SelectListItem {Text = x.Name,Value = x.Id.ToString()})。AsEnumerable(); 
@ Html.DropDownList( SelectedDocumentType,docTypes, - select - new {@class = ddSelect})



型号:

-----

  public  IEnumerable< doctypeentity> DocumentTypes { get ;  set ; } 



控制器:

-----------

 [HttpParamAction] 
[HttpPost]
[ValidateInput( false )]
public ActionResult Save()
{
int DocumentType = ???? // 我必须在此处指定下拉列表的选定值
}

解决方案

如果使用名为SelectedDocumentType的@ Html.DropDownList,请使用FormCollection,因为它将带来名称的所有值,即公共ActionResult Save(FormCollection fc),然后是fc [SelectedDocumentType ]应该给你价值。



另外,如果你有一个具有属性Document类型的Model,那么我更喜欢使用@ Html.DropdownListFor(Model.DocumentType)。更好的方法。



如果它有帮助,请喜欢。感谢。

Hi,

have a dropdown in my view, couple of other controls and a input button to save the data.
On saving I have ActionReult Save ()
which needs the value I selected in the dropdownlist.
How can I get the selected value of the dropdownlist on my controller Actionresult Save()

View:
------

var docTypes = Model.DocumentTypes.Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).AsEnumerable();
@Html.DropDownList("SelectedDocumentType", docTypes, "--select--", new { @class = "ddSelect" })


Model:
-----

public IEnumerable<doctypeentity> DocumentTypes { get; set; }


Controller:
-----------

[HttpParamAction]
[HttpPost]
[ValidateInput(false)]
public ActionResult Save()
{
int DocumentType = ????// I have to assign the selected value of the dropdownlist here
}

解决方案

If using @Html.DropDownList with a name "SelectedDocumentType", use FormCollection as it will bring all the values from view having name i.e. public ActionResult Save(FormCollection fc) and then fc["SelectedDocumentType"] should give you value.

Also, if you have a Model with property Document type, then I prefer using @Html.DropdownListFor(Model.DocumentType). Much better approach.

Please like if it helps. Thanks.


这篇关于如何将选定值从下拉列表中从视图传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆