MVC框架Enitity&DropDownListFor LT;> [英] MVC Enitity Framework DropDownListFor<>

查看:258
本文介绍了MVC框架Enitity&DropDownListFor LT;>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有搜索周围的网站解决方案,并同时尝试很多方法,我似乎无法得到这个工作的要求,我也想跟着来创建DropLists最佳MVC的最佳做法。

I have searched around the site for a solution and whilst trying numerous methods I cant seem to get this working as required, also I would like to follow best MVC best practise for creating DropLists.

我有3个型号(我已经削减下来为这个目的)

I have 3 Models (I have cut them down for the purposes of this)

示范一个
学生

Model One Student

public int ID {get;set;}
public string Name {get;set}
public Site SiteID {get;set;}

示范两个
站点

Model Two Site

public int ID {get;set;}
public string SiteName {get;set}

示范三
虚拟机

Model Three VM

public int ID {get;set}
public student Students {get;set;}

public DateTime Date  { get { return DateTime.Now; } }
public bool Criteria {get;set;}

在我的虚拟机视图我使用EditorFor HTML辅助来填充我的虚拟机和学生机型。该网站的模式是在数据库预种子填充。

In my VM view I am using EditorFor html helpers to populate my VM and Student Models. The site model is pre populated at the database seed.

我正在寻找最好的办法,包括对我的VM视图站点的一个DropDownList,将映射到我的学生模型。

I am looking for the best way to include a dropdownlist of sites on my VM view, that will map to my student model.

如何以正确设置我的模型来实现这一目标?

How to I correctly set up my models to achieve this?

提前很多感谢,
罗布

Many thanks in advance, Rob

推荐答案

在总之,你想要的 DropDownListFor 扩展方法,并把一个列表与LT;网站> 到视图模式。

In short, you want the DropDownListFor extension method and to put a List<Site> into the view model.

下面是一个小提琴演示您的案例的。小提琴有更多的细节。螺母和螺栓的位置:

Here is a Fiddle that demonstrates your case. The Fiddle has more details. The nuts and bolts are here:

public class MyViewModel
{
    public MyViewModel()
    {
        this.Sites = new List<Site>();
    }

    public int ID { get; set;}
    public Student Students { get; set; }

    public DateTime Date  { get { return DateTime.Now; } }
    public bool Criteria { get; set; }      

    public List<Site> Sites { get; set; }
}



查看 - 使用 DropDownListFor



View - Use DropDownListFor

@Html.DropDownListFor(m => m.Sites, 
    new SelectList(Model.Sites, "ID", "SiteName"))

在伪代码,上面说:


  • 模型网站对象包含显示的属性。

  • 创建一个新的的SelectList 使用该模型中的网站对象。使用 ID 属性作为数据值和站点名称属性作为数据文本。

  • 创建基于上述信息下拉列表。

  • The Sites object in the model contains the properties to display.
  • Create a new SelectList using the Sites object in the model. Use the ID property as the data value and the SiteName property as the data text.
  • Create a drop down list based on the above info.

这只是传递一个种子视图模型到视图。

This just passes a seeded view model to the view.

public ActionResult Index()
{
    var vm = SeedFromDatabase();
    return View(vm);
}

private MyViewModel SeedFromDatabase()
{
    var vm = new MyViewModel();
    vm.Sites.Add(new Site(0, "one"));
    vm.Sites.Add(new Site(1, "two"));
    vm.Sites.Add(new Site(2, "three"));
    return vm;
}

这篇关于MVC框架Enitity&DropDownListFor LT;&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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