【发布时间】:2011-04-28 15:02:02
【问题描述】:
我想将下拉列表的项目列表传递到强类型部分视图中。父视图使用 ViewModel 类,该类包含我要设置的值和相关下拉列表应包含的选项列表的属性。
我的 ViewModel 看起来像:
public class EntityFormViewModel
{
private MyDBEntity Entity { get; set; }
// *** The list of options. ***
public SelectList DataTypeSelectList { get; private set; }
public int EntityId { get; set; }
public byte FormatId { get; set; }
// *** The property the options relate to. ***
[UIHint("DataTypesDropDownST")]
public string DataType { get; set; }
// Other properties snipped...
public EntityFormViewModel(EntityModel db, int Id)
{
Entity = db.MyDBEntity.First(e => e.EntityId == Id);
DataTypeSelectList = new SelectList(db.SDDS_DataType.ToList(), "DataType", "Description", this);
EntityId = Entity.EntityId;
FormatId = Entity.FormatId;
DataType = Entity.DataType;
// Etc ...
}
public void Update(EntityModel db)
{
Entity.EntityId = EntityId;
Entity.FormatId = FormatId;
Entity.DataType = DataType;
// Etc ...
db.SaveChanges();
}
};
我在 EditorTemplates/DataTypesDropDown.ascx 中的部分视图看起来像
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApp.Controllers.EntityFormViewModel>" %>
<!-- Doesn't work. -->
<%= Html.DropDownListFor(m => Model, Model.DataTypeSelectList) %>
<!-- Doesn't work either. -->
<!-- <% Html.DropDownListFor(m => Model, Model.DataTypeSelectList); %> -->
我的父视图看起来像
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApp.Controllers.EntityFormViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<%: Html.EditorFor(m => m) %>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
通过传递 ViewData 字典中的项目列表,我可以让它在没有强类型的情况下正常工作,因此:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%: Html.DropDownList("", new SelectList(ViewData["DataTypes"] as IEnumerable, "DataType", "Description", Model)) %>
但我收到以下强类型错误:
传入字典的模型项是“System.String”类型的,但该字典需要一个“MvcApp.Controllers.EntityFormViewModel”类型的模型项。
似乎将错误的类型(字符串)传递到部分视图中,但是我可以做些什么来更改我传递的类型?还是我的方法完全错误?如果是这样,应该怎么做?我想这是一个非常常见的场景。
我知道我应该使用“存储库”类来访问数据库,但我想在围绕它构建太多其他东西之前让它工作。请注意,我的 ViewModel 位于控制器命名空间中。我想我应该把它移到 Model 命名空间,但我怀疑这是我的问题的原因。
================================================ ===================================
更新
我还是有问题,但至少我解决了编译器错误。我将视图模型更改如下
// Make this private
private string DataType { get; set; }
// Add this which supplies all the list information.
[UIHint("DataTypesDropDownST")]
public DataTypeOptsAndVal DataTypeAndOpts
{
get { return new DataTypeOptsAndVal(DataType, DataTypeSelectList); }
set { DataType = value.DataType; }
}
这个想法源于属性的返回值控制传递给局部视图的类型;整个模型没有通过,这是我之前的错误。因此,我返回一个包含部分视图所需的所有内容的类型。新的局部视图是
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApp.Controllers.DataTypeOptsAndVal>" %>
<%: Html.DropDownListFor(m => m.DataType, Model.DataTypeSelectList) %>
但是,现在呈现的表单完全没有此字段。我尝试在局部视图中包含静态文本,但即使这样也没有呈现。
任何帮助表示赞赏。强输入选项列表肯定不是那么难吗?
【问题讨论】:
标签: asp.net-mvc-2 partial-views