c# - MVC 3 : DropDownList on an Edit Form for an object that is a property of a ViewModel

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

概述: 我正在尝试使用具有属性的 ViewModel,该属性是我要编辑的模型类。我已经看到编辑表单在直接编辑模型时使用 MVC 脚手架编辑表单工作,但是我正在尝试使用包含正在编辑的模型的 ViewModel。除了保存显示在 DropDownList 中的字段外,一切正常。

解释: 我尝试使用 MVC 3 的脚手架功能为模型创建编辑表单。 在 MVC 音乐商店教程中,这是在 StoreManagerController 中为 Album 的编辑页面完成的。 在那个页面中,他们有两个下拉菜单,分别是流派和艺术家。每个在 View 中看起来都与此相似:-

<div class="editor-label">
    @Html.LabelFor(model => model.GenreId, "Genre")
</div>
<div class="editor-field">
    @Html.DropDownList("GenreId", String.Empty)
    @Html.ValidationMessageFor(model => model.GenreId)
</div>

据我所知,这些选项已使用 ViewBag 在 Controller 中填写。

ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);

直接使用模型

在我的代码中,我设法对通过 Entity Framework 保存到数据库的对象执行相同的操作。

模型

public class Season
{
    public int SeasonId { get; set; }
    public int ClubId { get; set; }
    public Club Club { get; set; }
}

Controller 中的代码

ViewBag.ClubId = new SelectList(clubs, "ClubId", "Name", season.ClubId);

查看

<div class="editor-label">
    @Html.LabelFor(model => model.ClubId, "Club")
</div>
<div class="editor-field">
    @Html.DropDownList("ClubId", String.Empty)
    @Html.ValidationMessageFor(model => model.ClubId)
</div>

这很好用。

使用 View 模型

但是现在我意识到页面需要显示的文本多于我正在编辑的模型中可用的文本。 我希望创建一个特殊的 View 模型,其中包含编辑后的模型(季节)和我想要显示的额外文本。

View 模型

public class EditSeasonViewModel
{
    public string PlayerName {get; set;}
    public string SummaryText {get; set;}
    public int PlayerId {get; set;}
    public Season season {get; set;}
}

我这样做了,更改了 Controller 以让 HttpGet 和 HttpPost 方法使用新的 ViewModel,更改了 View 以接受新的 ViewModel,并将 View 中的所有“EditorFor”方法更改为使用 model.season.MyProperty。

Controller 中的代码

ViewBag.ClubId = new SelectList(clubs, "ClubId", "Name", seasonVM.season.ClubId);

View 中的代码

<div class="editor-label">
    @Html.LabelFor(model => model.season.ClubId, "Club")
</div>
<div class="editor-field">
    @Html.DropDownList("ClubId", String.Empty)
    @Html.ValidationMessageFor(model => model.season.ClubId)
</div>

调试 HttpPost 方法时,季节的所有值都正确存在,但应来自 DropDropList 的 ClubId 值除外。

与直接使用模型时相比,我完全没有更改 View 中的 DropDownList。

问题: 我的问题是,在使用这个新的 EditSeasonViewModel 时,我需要更改什么才能正确保存 ClubId?

另外,controller中HttpGet方法中的ViewBag.ClubId如何匹配到View中的DropDownList,然后将其值传回HttpPost方法?

最佳答案

这是您的 DropDownList 声明不正确。试试这个:

@Html.DropDownListFor(m => m.season.ClubId, ViewBag.ClubId)

但是真的,您应该将该选择列表放入您的模型中:

public SelectList Clubs { get; set; }

然后将其填充到您的 Controller 中:

Model.Clubs = new SelectList(clubs, "ClubId", "Name", season.ClubId);

然后你可以这样做:

@Html.DropDownListFor(m => m.season.ClubId, Model.Clubs)

关于c# - MVC 3 : DropDownList on an Edit Form for an object that is a property of a ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15523478/

相关文章:

asp.net-mvc-3 - ASP.NET MVC3 : How to get access to parameters passed as routeValues anonymous type in a HtmlHelper extension method?

.net - 如何在 ASP.NET MVC 3 中发布文件数组?

wcf - 我可以在 Entity Framework 4.3 中使用 RIA 服务吗?

c# - Docker:dotnet ef数据库更新失败

可以执行循环中任何代码的 C# 方法

asp.net-mvc - MVC - 带查询字符串的路由

asp.net-mvc - 用于 Web 开发的 Silverlight 或 MVC

c# - 在一次调用中从多个表中进行选择

c# - 在 DB4O 中执行 "SELECT TOP n"

c# - 大锯齿状数组的快速初始化