如何从MVC中的选择列表中获取名称值 [英] How to get name value from selectlist in MVC

查看:134
本文介绍了如何从MVC中的选择列表中获取名称值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从mvc中的SelectList获取名称值而非ID值。



这是我的代码



控制器



 var occupList = new SelectList(new [] 
{
new {ID = 1 ,Name =Student},
new {ID = 2,Name =House Wife},
new {ID = 3,Name =Business Man},
new { ID = 4,Name =Service Man},
},
ID,Name,1);
ViewData [occupList] =占用列表;
返回查看();





查看



 @ Html.DropDownList(Occupation,ViewData [occupList]为SelectList,新{@ class =control-label,style =width:250px;})





现在,当我从下拉列表中选择项目时,它显示所选项目,但它将ID值存储在数据库中。



例如:我从下拉列表中选择学生姓名值并保存。现在,在数据库中它存储1值。



但是我不想存储ID值,即1.我想存储Name值,即Student在数据库中。



谢谢。



我的尝试:



我尝试过很多东西,但没有任何效果。

解决方案

引用:

但我不想存储ID值,即1.我想存储Name值,即学生在数据库中。



如果您不想要 ID 值,则将 Text 分配给 ID



 var occupList = new SelectList(new [] 
{
new {ID = 学生,姓名=学生},
新{ID =众议院妻子,姓名=众议院妻子},
新{ID =商人,姓名=商人 },
new {ID =Service Man,Name =Service Man},
},


名称不随帖子发送,因此您无法使用传统方法阅读。如果您的数据是硬编码的,那么您已经知道ID等于什么。



私人列表< MyData> GetData()
{
返回新的List< MyData> {
new MyData {ID = 1,Name =Student},
new MyData {ID = 2,Name =House Wife},
new MyData {ID = 3,Name =商人},
新MyData {ID = 4,姓名=服务人},
};
}

[HttpGet]
public ActionResult Index()
{
var occupList = new SelectList(GetData(),
ID ,姓名,1);

....

//当表格发布时获取ID然后从数据中获取名称
int postingID = 1;
string name = GetData()。FirstOrDefault(d => d.ID == postedID).Name;

....





编辑:当天,你*做*想要存储ID而不是文本,请相信我们,因为如果你不以这种方式维护数据完整性,它会回来并咬你。


I want to get the name value not ID value from SelectList in mvc.

Here is my code

Controller

var occupationList = new SelectList(new[] 
{
    new { ID = 1, Name = "Student" },
    new { ID = 2, Name = "House Wife" },
    new { ID = 3, Name = "Business Man" },
    new { ID = 4, Name = "Service Man" },
},
"ID", "Name", 1);
ViewData["occupationList"] = occupationList;
return View();



View

@Html.DropDownList("Occupation", ViewData["occupationList"] as SelectList, new {@class="control-label", style="width:250px;" })



Now, when I select the item from dropdownlist at that time it shows the selected item but it store the ID value in the database.

For ex : I select the Student Name value from the dropdownlist and save it. Now, in database it store the 1 value.

But I dont want to store the ID value i.e. 1. I want to store the Name value i.e. Student in database.

Thank You.

What I have tried:

I tried many thing but nothing worked.

解决方案

Quote:

But I dont want to store the ID value i.e. 1. I want to store the Name value i.e. Student in database.


if you dont want the ID value then assign the Text to ID

var occupationList = new SelectList(new[] 
{
    new { ID =  "Student", Name = "Student" },
    new { ID = "House Wife", Name = "House Wife" },
    new { ID = "Business Man", Name = "Business Man" },
    new { ID = "Service Man", Name = "Service Man" },
},


The name isn't sent with the post so you can't read it using traditional methods. If your data is hard-coded then you already know what the ID equates to.

private List<MyData> GetData()
{
    return new List<MyData> {
        new MyData{ ID=1, Name="Student" },
        new MyData{ ID=2, Name="House Wife" },
        new MyData{ ID=3, Name="Business Man" },
        new MyData{ ID=4, Name="Service Man" },
    };
}

[HttpGet]
public ActionResult Index()
{
    var occupationList = new SelectList(GetData(),
    "ID", "Name", 1);

    ....

    // when the form is posted get the ID then get the name from the data
    int postedID = 1;
    string name = GetData().FirstOrDefault(d => d.ID == postedID).Name;

    ....



Edit: By the day, you *do* want to store the ID and not the text, trust us on that as it'll come back and bite you if you don't maintain data integrity in this way.


这篇关于如何从MVC中的选择列表中获取名称值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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