【问题标题】:Create dropdown list of table创建表格的下拉列表
【发布时间】:2013-02-10 18:31:35
【问题描述】:

我目前有一个系列表。在这个表中,存在一个字段status_id,它是链接到表Status的外键。状态可以是新的、正在运行的、已结束的……

我的模型类看起来像这样:

public partial class Serie
{
    public Serie()
    {
        this.Episodes = new HashSet<Episode>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
    public Nullable<int> Status_id { get; set; }

    public virtual ICollection<Episode> Episodes { get; set; }
    public virtual Status Status { get; set; }
}

状态:

public partial class Status
{
    public Status()
    {
        this.Series = new HashSet<Serie>();
    }

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

    public virtual ICollection<Serie> Series { get; set; }
}

在编辑系列时,我想要一个包含所有可能状态(名称字段)的下拉列表,并在用户提交表单时收到此列表。完成此任务的最佳方法是什么?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-3 razor asp.net-mvc-4


    【解决方案1】:

    最简单的答案是您需要一个高管列表,将模型中的列表传递回您的视图,创建一个选择列表,然后呈现一个下拉列表。

    public ActionResult EditSomething()
    {
        Model Model = new Model();
        Model.statusList = {some method to fill IEnumerable<status>};
    
        return View(Model);
    }
    

    在你看来:

    @Html.DropDownListFor(model => model.Id, new SelectList(Model.statusList, "Id", "Name "))
    

    【讨论】:

      【解决方案2】:

      您需要使用&lt;%= Html.DropDownList("StatusId", Model.Statuses)%&gt; 创建下拉列表。您将使用所有可能的状态填充您的视图模型,记录用户在一个变量中选择的状态的 ID,该变量在用户提交表单时随您的视图模型一起发送。这是一篇详细介绍该过程的文章:http://odetocode.com/blogs/scott/archive/2010/01/18/drop-down-lists-and-asp-net-mvc.aspx

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-02
        • 2014-12-15
        • 2020-02-06
        • 2020-10-23
        • 1970-01-01
        • 2012-11-02
        相关资源
        最近更新 更多