ASP。从下拉列表中获取Id

时间:2022-11-15 04:01:56

I'm new in ASP.NET MVC and i have a problem. I needed to get a string "Category" from a dropdownlist, that was my code:

我刚在ASP。NET MVC和我有个问题。我需要从下拉列表中获得一个字符串“Category”,那是我的代码:

var CList = new List<string>();

StreetFooder sf = db.StreetFooders.Single(s => s.User.UserName == this.User.Identity.Name);

var Cqry = from d in db.Categories
where !d.IsDeleted && d.StreetFooder.Id == sf.Id
orderby d.Name
select d.Name;

CList.AddRange(Cqry.Distinct());
ViewBag.CategoryList = new SelectList(CList);

And the view:

和观点:

<div class="col-md-10">

@Html.DropDownListFor(model => model.Category, (SelectList)ViewBag.CategoryList, new { @class = "form-control" })

@Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-danger" })
</div>

And that worked for me. After that, i realized i made a mistake and changed my string Category attribute in:

这对我很有效。在那之后,我意识到我犯了一个错误,将我的string Category属性改为:

public virtual Categories Type { get; set; }

Now i want to save on my db the ID instead of string, but still want to see my string when select from dropdown list...but don't know how.

现在我想在db上保存ID而不是string,但是在从下拉列表中选择时仍然想看到我的字符串…但不知道如何。

3 个解决方案

#1


0  

Try this:

试试这个:

var cqry = from d in db.Categories
           where !d.IsDeleted && d.StreetFooder.Id == sf.Id
           orderby d.Name
           select new { Id = d.StreetFooder.Id, Value = d.Name};

ViewBag.CategoryList = new SelectList(cqry.Distinct(), "Id", "Value");

You need to also define Id and pass it to your DropDownList. This will initialize a new instance of the SelectList class by using the specified items for the list(cquery), the data value field(Id), and the data text field(Value) and add to you ViewBag then bind it to DropDownList.

您还需要定义Id并将其传递给下拉列表。这将通过使用列表(cquery)、数据值字段(Id)和数据文本字段(值)的指定项初始化SelectList类的一个新实例,然后将其绑定到DropDownList。

You can use it on View like this:

你可以这样使用它:

@Html.DropDownList("CategoryList ", null, new { @class = "form-control" })

cqry.Distinct() will work if you have duplicates on result, which means that the Id and Value in collection are same, if you need to distinct only by name, you need to use group by.

如果您在结果上有重复,那么cqry.Distinct()将发挥作用,这意味着集合中的Id和值是相同的,如果您只需要按名称进行区分,则需要使用group by。

#2


1  

Use SelectListItem to create the SelectList, there you can specify Value = your id and Text = display text

使用SelectListItem创建SelectList,在那里您可以指定值=您的id和文本=显示文本。

https://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem(v=vs.118).aspx

https://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem(v = vs.118). aspx

var CList = new List<SelectListItem>();

StreetFooder sf = db.StreetFooders.Single(s => s.User.UserName == this.User.Identity.Name);

var Cqry = from d in db.Categories
where !d.IsDeleted && d.StreetFooder.Id == sf.Id
orderby d.Name
select d;

CList.AddRange(Cqry.Distinct().Select(x => new SelectListItem 
{
    Value = x.Id.ToString(),
    Text = d.Name
}));
ViewBag.CategoryList = new SelectList(CList);

You can even omit using the SelectList and simply pass a List<SelectListItem> if I recall correctly.

您甚至可以省略SelectList,如果我没记错的话,只需传递List

#3


0  

In selectList, you can pass from which property you want to bind dropdown value and text property.

在selectList中,您可以传递想要绑定dropdown值和文本属性的属性。

 var Cqry = from d in db.Categories
    where !d.IsDeleted && d.StreetFooder.Id == sf.Id
    group d by new { names.Name } into nameGroup
    select new { Name = nameGroup.Name, Id = nameGroup.FirstOrDefault().StreetFooder.Id};


ViewBag.CategoryList = new SelectList(Cqry,"Id","Name");

You were selecting Distinct name, so I have used here group by name.

你选择的是不同的名字,所以我在这里按名字分组。

As I've created anonymous object with property 'Id' and Name. Now you can bind it in SelectList.

因为我创建了带有属性“Id”和名称的匿名对象。现在可以将其绑定到SelectList。

#1


0  

Try this:

试试这个:

var cqry = from d in db.Categories
           where !d.IsDeleted && d.StreetFooder.Id == sf.Id
           orderby d.Name
           select new { Id = d.StreetFooder.Id, Value = d.Name};

ViewBag.CategoryList = new SelectList(cqry.Distinct(), "Id", "Value");

You need to also define Id and pass it to your DropDownList. This will initialize a new instance of the SelectList class by using the specified items for the list(cquery), the data value field(Id), and the data text field(Value) and add to you ViewBag then bind it to DropDownList.

您还需要定义Id并将其传递给下拉列表。这将通过使用列表(cquery)、数据值字段(Id)和数据文本字段(值)的指定项初始化SelectList类的一个新实例,然后将其绑定到DropDownList。

You can use it on View like this:

你可以这样使用它:

@Html.DropDownList("CategoryList ", null, new { @class = "form-control" })

cqry.Distinct() will work if you have duplicates on result, which means that the Id and Value in collection are same, if you need to distinct only by name, you need to use group by.

如果您在结果上有重复,那么cqry.Distinct()将发挥作用,这意味着集合中的Id和值是相同的,如果您只需要按名称进行区分,则需要使用group by。

#2


1  

Use SelectListItem to create the SelectList, there you can specify Value = your id and Text = display text

使用SelectListItem创建SelectList,在那里您可以指定值=您的id和文本=显示文本。

https://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem(v=vs.118).aspx

https://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem(v = vs.118). aspx

var CList = new List<SelectListItem>();

StreetFooder sf = db.StreetFooders.Single(s => s.User.UserName == this.User.Identity.Name);

var Cqry = from d in db.Categories
where !d.IsDeleted && d.StreetFooder.Id == sf.Id
orderby d.Name
select d;

CList.AddRange(Cqry.Distinct().Select(x => new SelectListItem 
{
    Value = x.Id.ToString(),
    Text = d.Name
}));
ViewBag.CategoryList = new SelectList(CList);

You can even omit using the SelectList and simply pass a List<SelectListItem> if I recall correctly.

您甚至可以省略SelectList,如果我没记错的话,只需传递List

#3


0  

In selectList, you can pass from which property you want to bind dropdown value and text property.

在selectList中,您可以传递想要绑定dropdown值和文本属性的属性。

 var Cqry = from d in db.Categories
    where !d.IsDeleted && d.StreetFooder.Id == sf.Id
    group d by new { names.Name } into nameGroup
    select new { Name = nameGroup.Name, Id = nameGroup.FirstOrDefault().StreetFooder.Id};


ViewBag.CategoryList = new SelectList(Cqry,"Id","Name");

You were selecting Distinct name, so I have used here group by name.

你选择的是不同的名字,所以我在这里按名字分组。

As I've created anonymous object with property 'Id' and Name. Now you can bind it in SelectList.

因为我创建了带有属性“Id”和名称的匿名对象。现在可以将其绑定到SelectList。