MVC3 控制器如何获得view中DropDownList选中的值

lk_cqu 2011-05-16 10:26:52
在view中有一个dropdownList以及一个按钮,当单击按钮时如何把dropdownList中选中的值传到控制器中?
...全文
837 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
saisky 2011-06-15
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 lk_cqu 的回复:]

引用 13 楼 zmcici 的回复:


噢,忘了改了。view代码我是copy 5楼的。我也是刚接触.net的,对get post方式确实还不太了解
[/Quote]
这跟刚接触不刚接触 .net有关系么?貌似html基础知识吧?哎,现在上道的兄弟们都是裸奔上来的,html都不学就开干了。。
gengchenhui 2011-05-18
  • 打赏
  • 举报
回复
做webform把最基本的东西都丢了。。。
hitlcyu19 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 lk_cqu 的回复:]

我用的是mvc3
[/Quote]
服了。。。
lk_cqu 2011-05-17
  • 打赏
  • 举报
回复
我用的是mvc3
lk_cqu 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wlf535944903 的回复:]
[/Quote]
帮我看一下我的代码

@using (Html.BeginForm()) {
<div>
<fieldset>
<p>端口:@Html.DropDownList("listchannel", ViewBag.list as IEnumerable<SelectListItem>)</p>
<p><input type="submit" value="保存" /></p>
</fieldset>
</div>
}



public ActionResult Open()
{
List<SelectListItem> items = new List<SelectListItem>();
int i = 1;
foreach (string s in SerialPort.GetPortNames())
{
items.Add(new SelectListItem { Text = s, Value = (i++).ToString() });
}
ViewBag.list = items;
return View();
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Open(FormCollection collection)
{

Response.Write(int.Parse(Request.Form["list"]));
return View();
}
public class Port
{
public int id { get; set; }
public string Portname { get; set; }
}

选定一个端口点确定后就报错了,Response.Write(int.Parse(Request.Form["list"]));
值不能为 null。
参数名: String

请问一下改怎么修改?
我的本意是想要获得端口名,上面的即使是没出错也只是获得一个id而不是端口名
hitlcyu19 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sandy945 的回复:]

Request.Form[dropdownList.Name]

or

直接用 html.dropdownList

action 用viewmodel接收
[/Quote]
FormCollection也可以
lk_cqu 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wlf535944903 的回复:]
C# code


[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ColumnManage(string columnname,string sort)
{
try
{
ViewData["listchannel"] ……
[/Quote]
m00.ChannelID 这个m00好像还没定义吧?
wlf535944903 2011-05-17
  • 打赏
  • 举报
回复


[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ColumnManage(string columnname,string sort)
{
try
{
ViewData["listchannel"] = new SelectList(b00.ListChannel(), "ChannelID", "ChannelName");
m00.ChannelID = int.Parse(Request.Form["listchannel"]);
Response.Write(m00.ChannelID);
return View();
}
catch
{
return View();
}
}

wlf535944903 2011-05-17
  • 打赏
  • 举报
回复

<% SelectList categories = ViewData["listchannel"] as SelectList; %>
<% using (Html.BeginForm()) { %>
<div>
<fieldset>
<legend>添加栏目</legend>
<p>栏目名称:<input type="text" id="columnname" style=" width:100px;"/></p>
<p>上级目录:<select><option></option></select></p>
<p>排      序:<input type="text" id="sort" style=" width:30px;"/></p>
<p>频      道:<%=Html.DropDownList("listchannel", categories)%></p>
<p><input type="submit" value="保存" /></p>
</fieldset>
</div>
<% } %>


claymore1114 2011-05-17
  • 打赏
  • 举报
回复
一般就 post 和get 两种方式, 还不知道传?
阿非 2011-05-17
  • 打赏
  • 举报
回复
Request.Form[dropdownList.Name]

or

直接用 html.dropdownList

action 用viewmodel接收
lk_cqu 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 zmcici 的回复:]

[/Quote]
噢,忘了改了。view代码我是copy 5楼的。我也是刚接触.net的,对get post方式确实还不太了解
骑猪看海 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lk_cqu 的回复:]
引用 6 楼 wlf535944903 的回复:

帮我看一下我的代码

HTML code

@using (Html.BeginForm()) {
<div>
<fieldset>
<p>端口:@Html.DropDownList("listchannel", ViewBag.list as IEnu……
[/Quote]
Response.Write(int.Parse(Request.Form["list"]));

Response.Write(int.Parse(Request.Form["listchannel"])); 吧
list哪存在
骑猪看海 2011-05-17
  • 打赏
  • 举报
回复
做MVC 如果连get post都不知道是什么,有什么用。那趁早回去做webform吧。
lk_cqu 2011-05-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wxr0323 的回复:]
可以用URL传
[/Quote]
能否说得清楚点呢?
子夜__ 2011-05-16
  • 打赏
  • 举报
回复
可以用URL传

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧