ASP.NET MVC - 如何使用另一个模型填充下拉列表?

发布于 2024-11-26 02:21:25 字数 2004 浏览 1 评论 0原文

一个称为 PropertyModel,另一个称为 PropertyTypeModelPropertyModel 包含一个 PropertyTypeModel,如下所示:

public class PropertyModel  {
    public int PropertyID { get; set; }

    public PropertyTypeModel PropertyType { get; set; }

    [DataType(DataType.Text)]
    [DisplayName("Property name")]
    public string PropertyName { get; set; }
}

PropertyTypeModel 是这样的:

public class PropertyTypeModel {
    public int PropertyTypeID { get; set; }
    [DataType(DataType.Text)]

    [DisplayName("Property type")]
    public string PropertyType { get; set; }

    public static List<SelectListItem> PropertyTypeSelectList()
    {
        using (Properties dataContext = new Properties())
        {
            return (from pt in dataContext.PropertyTypes
                    select new SelectListItem
                    {
                        Value = pt.PropertyTypeID.ToString(),
                        Text = pt.PropertyTypeName
                    }).ToList();
        }          
    }
}

PropertyTypeModel 从数据库读取,并创建一个包含(目前)两个值“house”和“apartment”的列表。在视图中,我需要从 dropdownlist 中选择属性类型,我能够做到这一点的唯一方法是将列表直接硬编码到视图中,如下所示:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PropertyApplication.Models.PropertyModel>" %>
....
....
<%= Html.DropDownListFor(m => m.PropertyType.PropertyType,  new[] {
new SelectListItem { Text = "House",
Value = "House" },
new SelectListItem { Text = "Apartment",
Value = "Apartment" }
}
, "Choose one") %>

我不'不希望这样,因为在数据库中进行的任何更改(例如添加另一种类型的属性)都意味着重新编码视图中的列表。此外,这段代码是这样给我的,我需要使用 PropertyTypeModel

我的问题是:如何使用 PropertyTypeModel PropertyTypeSelectList 填充 dropdownlist ?我无法找到任何有关如何实现这一目标的信息。如何将类型模型“读”入属性模型?

请帮忙,我已经处理这个问题好几个小时了。如果可能的话,有这样做的代码,那就太好了。

one is called PropertyModel and the other is called PropertyTypeModel. The PropertyModel contains a PropertyTypeModel as you can see next:

public class PropertyModel  {
    public int PropertyID { get; set; }

    public PropertyTypeModel PropertyType { get; set; }

    [DataType(DataType.Text)]
    [DisplayName("Property name")]
    public string PropertyName { get; set; }
}

The PropertyTypeModel is this:

public class PropertyTypeModel {
    public int PropertyTypeID { get; set; }
    [DataType(DataType.Text)]

    [DisplayName("Property type")]
    public string PropertyType { get; set; }

    public static List<SelectListItem> PropertyTypeSelectList()
    {
        using (Properties dataContext = new Properties())
        {
            return (from pt in dataContext.PropertyTypes
                    select new SelectListItem
                    {
                        Value = pt.PropertyTypeID.ToString(),
                        Text = pt.PropertyTypeName
                    }).ToList();
        }          
    }
}

The PropertyTypeModel reads from the database, and creates a list with (for now) two values, "house" and "apartment". in the view, where i need to select the property type from a dropdownlist, the only way i've been able to do so is by hardcoding the list straight into the view, like so:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PropertyApplication.Models.PropertyModel>" %>
....
....
<%= Html.DropDownListFor(m => m.PropertyType.PropertyType,  new[] {
new SelectListItem { Text = "House",
Value = "House" },
new SelectListItem { Text = "Apartment",
Value = "Apartment" }
}
, "Choose one") %>

I don't want this, since any changes made in the database, such as adding another type of property will mean re-coding the list in the view. besides, this code was given to me like this, and I NEED to use the PropertyTypeModel.

My question is: How do i populate the dropdownlist for with the PropertyTypeModel PropertyTypeSelectList? I haven't been able to find any info on how to achieve this. How to "read" the type model into the property model?

Please help, i've been at this for hours. If possible at all, the code to do so, that would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冧九 2024-12-03 02:21:25

你尝试过吗

<%= Html.DropDownListFor(m => m.PropertyType.PropertyType,  Model.PropertyType.PropertyTypeSelectList(), "Choose one") %>

Have you tried

<%= Html.DropDownListFor(m => m.PropertyType.PropertyType,  Model.PropertyType.PropertyTypeSelectList(), "Choose one") %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文