我可以使用一些帮助实现@ Html.DropDownListFor.我的目标是按类别过滤产品列表.
此代码将显示一个列表框:
@model IEnumerable<Sample.Models.Product>
@{
List<Sample.Models.Category> list = ViewBag.Categories;
var items = new SelectList(list, "CategoryID", "CategoryName");
}
@Html.DropDownList("CategoryID", items)
Run Code Online (Sandbox Code Playgroud)
但我@Html.DropDownListFor上班有困难:
@model IEnumerable<Sample.Models.Product>
@{
List<Sample.Models.Category> list = ViewBag.Categories;
var items = new SelectList(list, "CategoryID", "CategoryName");
}
@Html.DropDownListFor(???, @items)
Run Code Online (Sandbox Code Playgroud)
我可以使用一些帮助构建Linq部分@Html.DropDownListFor.这是模型:
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int CategoryID { get; set; }
public string QuantityPerUnit { get; set; }
public Decimal? UnitPrice { get; …Run Code Online (Sandbox Code Playgroud) 我已经在www.asp.net上完成了关于MVC 3的新教程(音乐商店) .一切都很顺利,除了应该从数据库中填充两个下拉框的部分 - 而它们不是.
我按照教程并仔细检查了我的代码.我认为问题可能是使用editorstemplate文件夹.因为我是MVC的新手,所以不知道.那么问题是什么,或者我该如何调试呢?
==============
好吧所以这里是album.cshtml的一些代码,它位于/ views/shared/editortemplates /文件夹中
@model MvcMusicStore.Models.Album
<p> @Html.LabelFor(model => model.Genre) @Html.DropDownList("GenreId",
new SelectList(ViewBag.Genres as System.Collections.IEnumerable,
"GenreId", "Name", Model.GenreId))
</p>
<p> @Html.LabelFor(model => model.Artist) @Html.DropDownList("ArtistId",
new SelectList(ViewBag.Artists as System.Collections.IEnumerable,
"ArtistId", "Name", Model.ArtistId))
</p>
Run Code Online (Sandbox Code Playgroud)
我相信其中包括:
public ActionResult Edit(int id)
{ ViewBag.Genres = storeDB.Genres.OrderBy(g => g.Name).ToList(); ViewBag.Artists = storeDB.Artists.OrderBy(a => a.Name).ToList();
var album = storeDB.Albums.Single(a => a.AlbumId == id);
return View(album);
}
Run Code Online (Sandbox Code Playgroud)
除了下拉列表没有填充之外我没有任何错误...
==============
所以我在/views/storemanager/edit.cshtml中有edit.cshtml,然后在/views/shared/editortemplates/album.cshtml中有album.cshtml.下拉列表应该从album.cshtml填充到edit.cshtml中.我将album.cshtml中的代码直接放到edit.cshtml中,它运行正常.所以我认为问题是editortemplates/album.cshtml无效,即填充edit.cshtml页面.什么给出了什么?谢谢...
==============
好的,我发现了问题,我从CodePlex获得了工作源.好像我没有正确设置create.cshtml和edit.cshtml页面.无论如何所有现在都修好了,谢谢......
我的困难是如何使用ViewBag与DropdownListFor?
在我的控制器中,我有:
TestModel model = new TestModel();
ViewBag.Clients = model.Clients;
ViewBag.StatusList = model.StatusList;
ViewBag.enumStatus = model.enumStatus;
ViewBag.intClient = model.intClient;
Run Code Online (Sandbox Code Playgroud)
在我的TestModel中
public SelectList Clients { get; set; }
public SelectList StatusList { get; set; }
public ActiveStatus enumStatus { get; set; }
public int? intClient { get; set; }
Run Code Online (Sandbox Code Playgroud)
在我看来
我想用来DropDownListFor显示ViewBag值,我该怎么做?