ASP.NET MVC - 如何使用另一个模型填充下拉列表?
一个称为 PropertyModel
,另一个称为 PropertyTypeModel
。 PropertyModel
包含一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你尝试过吗
Have you tried