【问题标题】:ASP.NET MVC 2 strongly typed partial view for foreign key list - what type gets passed in?外键列表的 ASP.NET MVC 2 强类型部分视图 - 传入什么类型?
【发布时间】: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


    【解决方案1】:

    好的,既然我在这里没有答案,我想这里没有人知道!但是,有一个答案,并记录在这里:

    http://www.dalsoft.co.uk/blog/index.php/2010/04/26/mvc-2-templates/#Complex_Types

    我完全正确,因为默认的 MVC 框架模板匹配只支持简单类型。然而,简单的解决方案就是告诉它使用什么模板:

    <%: Html.EditorFor(m => m.DataTypeAndOpts, "DataTypesDropDownST") %>
    

    “DataTypesDropDownST”是部分视图名称。

    但是,为什么它不适用于 EditorForModel() 和 [UIHint] 我仍然不知道。 (当然,UIHint 和上面的代码做的一模一样?)

    【讨论】:

      【解决方案2】:

      在您的主视图中,而不是:

      <%: Html.EditorFor(m => m) %>
      

      使用:

      <%: Html.EditorForModel() %>
      

      它是等效的,但更具可读性。

      至于编辑器模板未呈现的原因,您在模型上使用了 UIHint 属性。这意味着它将寻找~/Views/Home/EditorTemplates/DataTypesDropDownST.ascx。确保该文件存在于该位置并包含您在更新中显示的代码。

      【讨论】:

      • Darin,非常感谢您花时间回答我的问题,并传递 EditorForModel() 提示。我已经根据您的建议更新了源代码。我的 DataTypesDropDownST.ascx 文件已经在您建议的位置。我怀疑它没有被渲染,因为关联的属性不是简单的类型,所以从下拉列表返回的值不能转换为属性的类型,即使该属性只关心字符串。
      猜你喜欢
      • 1970-01-01
      • 2011-02-23
      • 2010-10-12
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多