如何将选定的下拉值从视图传递到控制器? [英] How to pass selected dropdown value from view to controller?

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

问题描述

亲爱的,

如何将视图中的值传递给控制器​​,即

我有一个静态下拉列表,我创建如下。



1)控制器代码



  var  list =  new  SelectList( new  [] 
{
new {ID = 1, Name = name1},
new {ID = 2,Name = name2},
new {ID = 3,Name = name3},
},
ID 名称 1 );

ViewData [ list] = list;





2)在我的视图中,我按如下方式绑定Dropdown:

@ Html.DropDownList(list,ViewData [ list]作为SelectList)



如何从视图中获取下拉列表的选定值并将其传递给控制器​​?



我尝试了以下但是没有用。

  string  sTest = Request.Form [  list]。ToString(); 





我尝试过:



string sTest = Request.Form [ list]。ToString();

解决方案

看看这里:

通过下拉列表在MVC中选择的值(从视图到控制器) [ ^ ]

Dear All,
How can I Pass the Value from the View to Controller i.e.
I have a static Dropdown which I have created as follows.

1) Controller Code

var list = new SelectList(new[] 
                {
                    new { ID = "1", Name = "name1" },
                    new { ID = "2", Name = "name2" },
                    new { ID = "3", Name = "name3" },
                },
                "ID", "Name", 1);

                ViewData["list"] = list;



2) In My View I am binding the Dropdown as follows:
@Html.DropDownList("list", ViewData["list"] as SelectList)

how can I get the selected value of the dropdown from view and pass it to a controller?

I tried the following but it did not work.

string sTest = Request.Form["list"].ToString();



What I have tried:

string sTest = Request.Form["list"].ToString();

解决方案

Have a look here :
Pass Dropdownlist Selected Value In MVC(From View To Controller)[^]


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

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