绑定数据到DropDownList MVC剃刀 [英] Binding Data To DropDownList MVC Razor
问题描述
专业化模型:
public class SpecializationModel
{
[Display(Name =SpecializationID)]
public string SpecializationID {get;组; }
[Display(Name =SpecializationDescription)]
public string SpecializationDescription {get;组; }
public IEnumerable< SelectListItem>物品{get;组; }
public int SelectedSpecializationID {get;组;
}
控制器:
public ActionResult Physicians()
{
SpecializationManager spec = new SpecializationManager();
列表<专业化> SpecializationList = spec.GetAllSpecialization();
var obj = new SpecializationModel();
obj.Items = new []
{
foreach(SpecializationList中的var x)
{
new SelectListItem {Value = x.SpecializationID.ToString (),Text = x.SpecializationDescription};
}
};
return View(obj);
}
我有这个经理,其中包含我的LINQ查询从数据库中提取数据。
我在控制器上遇到问题。其中在 foreach
语法中的错误点说无效的表达式术语foreach
$ b $任何人都可以指出我正确的方向吗?非常感谢!
编辑:
我现在在foreach部分没有错误的代码(谢谢下面我结合我以上的职位)。但是,我似乎不能使最后一行工作。它会产生一个关于隐式转换的错误:
var items = new List< SelectListItem>();
foreach(SpecializationList中的var x)
{
items.Add(new SelectListItem {Value = x.SpecializationID.ToString(),Text = x.SpecializationDescription});
}
obj.Items = items.ToList();
请帮助我。谢谢:)
yopu不能在构造函数中抛出一个foreach,请尝试:
var items = new List< SelectListItem>();
foreach(SpecializationList中的var x)
{
items.add(new SelectListItem {Value = x.SpecializationID.ToString(),Text = x.SpecializationDescription});
}
obj.Items = items;
已编辑
I have just started with my project using MVC and Razor. Now I am encountering a problem when it comes to binding data coming from the database to a dropdownlist. Please refer on my codes below:
Specialization Model:
public class SpecializationModel
{
[Display(Name = "SpecializationID")]
public string SpecializationID { get; set; }
[Display(Name = "SpecializationDescription")]
public string SpecializationDescription { get; set; }
public IEnumerable<SelectListItem> Items { get; set; }
public int SelectedSpecializationID { get; set; }
}
Controller:
public ActionResult Physicians()
{
SpecializationManager spec = new SpecializationManager();
List<Specialization> SpecializationList = spec.GetAllSpecialization();
var obj = new SpecializationModel();
obj.Items = new[]
{
foreach(var x in SpecializationList)
{
new SelectListItem { Value = x.SpecializationID.ToString(), Text = x.SpecializationDescription };
}
};
return View(obj);
}
I have this manager which contains my LINQ query to extract the data from the database.
I encounter problems on the controller. Wherein the error points on the foreach
syntax saying Invalid expression term foreach
Can anyone please point me to the right direction? Thanks a lot!
EDIT:
I have this code now without errors on the foreach part (thanks to the post below which I combined with what I have above). However, I can't seem to make the last line work. It produces an error about implicit cast:
var items = new List<SelectListItem>();
foreach (var x in SpecializationList)
{
items.Add(new SelectListItem { Value = x.SpecializationID.ToString(), Text = x.SpecializationDescription });
}
obj.Items = items.ToList();
Please do help me. Thanks :)
yopu can't put a foreach in a constructor, try:
var items = new List<SelectListItem >();
foreach(var x in SpecializationList)
{
items.add(new SelectListItem { Value = x.SpecializationID.ToString(), Text = x.SpecializationDescription });
}
obj.Items = items;
Edited
这篇关于绑定数据到DropDownList MVC剃刀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!