绑定数据到DropDownList MVC剃刀 [英] Binding Data To DropDownList MVC Razor

查看:151
本文介绍了绑定数据到DropDownList MVC剃刀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用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屋!

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