MVC 中来自数据库文本、值的下拉列表
Drop down list in MVC from DB text, value
我对下拉列表有疑问。我想在 selectedlist 但 select Id 中显示文本。
我如何实现视图?鉴于我必须将 selected 数据保存到模型 (odbiorca)
控制器:
var odbiorca = dbU.Uczniowie.OrderBy(d => d.Nazwisko).Select(m => m.Nazwisko + " " + m.Imie).ToList(); //return Names
var odbiorcaId= dbU.Uczniowie.OrderBy(d => d.Id).Select(m => m.Nazwisko + " " + m.Imie).ToList(); //return IDs
List<SelectListItem> items = new List<SelectListItem>();
for (int i=0;i<dbU.Uczniowie.Count();i++)
{
items.Add(new SelectListItem { Text = odbiorca[i], Value = odbiorcaId[i] });
}
ViewBag.odbiorca = items;
型号:
public class Uwaga
{
[Key]
[Required]
public string Id { get; set; }
[Required]
public string odbiorca { get; set; }
[Required]
public DateTime data { get; set; }
}
我试过这个解决方案,但程序保存了文本,但没有保存值。
@Html.DropDownList("odbiorca", "----")
你的两个 linq 语句 return 相同的值。
var odbiorca = dbU.Uczniowie.OrderBy(d => d.Nazwisko).Select(m => m.Nazwisko + " " + m.Imie).ToList(); //return Names
var odbiorcaId= dbU.Uczniowie.OrderBy(d => d.Id).Select(m => m.Nazwisko + " " + m.Imie).ToList(); //return IDs
这两个return一个列表m.Nazwisko + " " + m.Imie
没有Id.
您应该只使用一个查询来获取您的 select 列表
var list = dbU.Uczniowie.Select(a => new {Text= a.Nazwisko + " " + a.Imie, Value = a.Id}).ToList();
ViewBag.odbiorca = list.Select(a => new SelectListItem{ Text = a.Text, Value = a.Value.ToString()});
试试这个方法。让我们采用您的模型 class:
public class Uwaga
{
[Key]
[Required]
public string Id { get; set; }
Required]
public string odbiorca { get; set; }
[Required]
public DateTime data { get; set; }
}
在视图中您可以使用相同的助手:@Html.DropDownList("odbiorca", "----")
但是,在控制器中,您通过 SelectList
对象设置了 "id" 和 "text" 列。以控制器中的默认动作为例:
public ActionResult Index()
{
//Creates a example list with multiple "Uwaga" items,
//but you might want to pull the data from the database
List<Uwaga> UwagaList = new List<Uwaga>();
for (int i = 1; i <= 10; i++)
{
Uwaga item = new Uwaga();
item.Id = i;
item.odbiorca = "odbiorca " + i;
UwagaList.Add(item);
}
//Create a SelectList object, the first parameter is the list created above.
//The second parameter is the "id" key and the thirth
//parameter is the text you want to display in the dropdown.
var SelectList = new SelectList(UwagaList, "Id", "odbiorca");
//Fill the dropdownlist "odbiorca" using ViewData.
ViewData["odbiorca"] = SelectList;
}
希望对您有所帮助。
我对下拉列表有疑问。我想在 selectedlist 但 select Id 中显示文本。 我如何实现视图?鉴于我必须将 selected 数据保存到模型 (odbiorca)
控制器:
var odbiorca = dbU.Uczniowie.OrderBy(d => d.Nazwisko).Select(m => m.Nazwisko + " " + m.Imie).ToList(); //return Names
var odbiorcaId= dbU.Uczniowie.OrderBy(d => d.Id).Select(m => m.Nazwisko + " " + m.Imie).ToList(); //return IDs
List<SelectListItem> items = new List<SelectListItem>();
for (int i=0;i<dbU.Uczniowie.Count();i++)
{
items.Add(new SelectListItem { Text = odbiorca[i], Value = odbiorcaId[i] });
}
ViewBag.odbiorca = items;
型号:
public class Uwaga
{
[Key]
[Required]
public string Id { get; set; }
[Required]
public string odbiorca { get; set; }
[Required]
public DateTime data { get; set; }
}
我试过这个解决方案,但程序保存了文本,但没有保存值。 @Html.DropDownList("odbiorca", "----")
你的两个 linq 语句 return 相同的值。
var odbiorca = dbU.Uczniowie.OrderBy(d => d.Nazwisko).Select(m => m.Nazwisko + " " + m.Imie).ToList(); //return Names
var odbiorcaId= dbU.Uczniowie.OrderBy(d => d.Id).Select(m => m.Nazwisko + " " + m.Imie).ToList(); //return IDs
这两个return一个列表m.Nazwisko + " " + m.Imie
没有Id.
您应该只使用一个查询来获取您的 select 列表
var list = dbU.Uczniowie.Select(a => new {Text= a.Nazwisko + " " + a.Imie, Value = a.Id}).ToList();
ViewBag.odbiorca = list.Select(a => new SelectListItem{ Text = a.Text, Value = a.Value.ToString()});
试试这个方法。让我们采用您的模型 class:
public class Uwaga
{
[Key]
[Required]
public string Id { get; set; }
Required]
public string odbiorca { get; set; }
[Required]
public DateTime data { get; set; }
}
在视图中您可以使用相同的助手:@Html.DropDownList("odbiorca", "----")
但是,在控制器中,您通过 SelectList
对象设置了 "id" 和 "text" 列。以控制器中的默认动作为例:
public ActionResult Index()
{
//Creates a example list with multiple "Uwaga" items,
//but you might want to pull the data from the database
List<Uwaga> UwagaList = new List<Uwaga>();
for (int i = 1; i <= 10; i++)
{
Uwaga item = new Uwaga();
item.Id = i;
item.odbiorca = "odbiorca " + i;
UwagaList.Add(item);
}
//Create a SelectList object, the first parameter is the list created above.
//The second parameter is the "id" key and the thirth
//parameter is the text you want to display in the dropdown.
var SelectList = new SelectList(UwagaList, "Id", "odbiorca");
//Fill the dropdownlist "odbiorca" using ViewData.
ViewData["odbiorca"] = SelectList;
}
希望对您有所帮助。