充分利用一个asp.net MVC的DropDownList值 [英] Getting values from an asp.net mvc dropdownlist

查看:110
本文介绍了充分利用一个asp.net MVC的DropDownList值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我在asp.net mvc的从一个DropDownList获得价值?

Can someone help me with getting values from a dropdownlist in asp.net mvc?

我可以从文本框等价值......但是,我怎么得到这些两件事情...

I can get values from textboxes,etc...but,how do I get these 2 things...


  1. 的下拉列表,从控制器类中获取选定的项目值

  2. 获取掉落物品的所有列表中向下从控制器类列表

感谢

推荐答案

您可以从下拉列表中,你的文本框做得到选定的值是一样的。

You can get the selected value from a drop down list the same way as you do for text boxes.

使用默认的模型绑定

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GetValueExample(string MyList) {
  //MyList will contain the selected value
  //...
}

或从的FormCollection

or from a FormCollection

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GetValueExample(FormCollection form) {
  string val = form["MyList"];
  //...
}

或从请求

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GetValueExample(string MyList) {
  string val = Request.Form["MyList"];  //or
  val = Request["MyList"];
  //...
}

在您的下拉列表被命名为MYLIST。

Where your drop down list is named "MyList".

<%= Html.DropDownList("MyList", MyItems) %>

或直HTML

<select name="MyList">
  <option value="1">Item 1</option>
  <option value="2">Item 2</option>
</select>

浏览器将只从下拉列表中,而不是所有的其他值提交选定的值。为了让您应该调用code,它填充在首位列表中的所有其他项目的列表(假设你使用Html.DropDownList())。

The browser will only submit the selected value from the drop down list and not all the other values. To get the list of all the other items you should invoke the code that populated the list in the first place (assuming you used Html.DropDownList()).

更新

[AcceptVerbs(Http.Get)]
public ActionResult GetValueExample() {
  ViewData["MyItems"] = GetSelectList();
  return View();
}

[AcceptVerbs(Http.Get)]
public ActionResult GetValueExample(string MyList) {
  //MyList contains the selected value
  SelectList list = GetSelectList(); //list will contain the original list of items
  //...
}

private SelectList GetSelectList() {
  Dictionary<string, string> list = new Dictionary<string, string>();
  list.Add("Item 1", "1");
  list.Add("Item 2", "2");
  list.Add("Item 3", "3");
  return new SelectList(list, "value", "key");
}

//...

<%= Html.DropDownList("MyList", ViewData["MyItems"] as SelectList) %>

这篇关于充分利用一个asp.net MVC的DropDownList值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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