几个星期以来,我一直在努力解决一件本应非常简单的事情。我只想在asp.net mvc 3 razor html页面中创建一个dropdownlist,并且我希望dropdownlists的数据来自一个模型。
我的模型如下,位于Models.Project命名空间中。
public class Project
{
public Project()
{
CategoryId = 0;
Name = "";
Description = "";
//Categories = new Dictionary<int, string>();
Entities _db = new Entities(); //ef4
CateogoriesList = from c in _db.Categories
orderby c.Name
select c.Name;
}
public int CategoryId { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Project Name")]
public string Name { get; set; }
[Required]
[DataType(DataType.MultilineText)]
[Display(Name = "Project Description")]
public string Description { get; set; }
public IQueryable<string> CateogoriesList;
}
我的控制器操作如下
public ActionResult Create()
{
Models.Project.Project proj = new Models.Project.Project();
return View(proj);
}
我的Razor视图有以下相关代码。。。
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@model Models.Project.Project
@using (Html.BeginForm())
{
@Html.ValidationSummary(true);
<fieldset>
<legend>Submit Your Request</legend>
<div class="editor-label">@Html.LabelFor( Model => Model.CateogoriesList )</div>
<div class="editor-field">
@Html.DropDownList("Category", new SelectList( Model.CateogoriesList ) )
</div>
</fieldset>
<p><input type="submit" value="Send for RFP" /></p>
}
问题是我得到了以下错误。。。
Compiler Error Message: CS0135: 'Model' conflicts with the declaration 'System.Web.Mvc.WebViewPage<TModel>.Model'
我看到下面的剪辑使它与ViewBag一起工作。。。我不明白为什么当我把列表包括在模型中时它就不起作用了。
http://www.pluralsight-training.net/microsoft/players/PSODPlayer.aspx?author=scott-allen&name=mvc3-构建-data-i&mode=live&clip=0&课程=aspdotnet-mvc3-介绍
我也看到有很多人似乎在这个简单的任务上遇到了麻烦,但在我的谷歌搜索中。。。我还没有遇到过任何人在创建下拉列表时出现同样的错误。
如果您或任何人有任何建议,我将不胜感激。我唯一想到的是SelectList
构造函数接受一个类型为System.Collections.IEnumerable
的参数,我试图传递它的是S ystem.Collections.Generic.IEnumerable
。。。或者接近它的东西。。。我不知道该怎么演才合适。。。尽管我认为我不应该。。。如果它使用viewbag作为交通工具,为什么它不使用模型作为交通工具?
谢谢,
编辑:==========================
问题在于selectList构造函数将接受的对象类型。出于某种原因,它不接受通用的IQueryable,但当我使用强制转换扩展方法将实体框架的结果强制转换为Array时,它突然起了作用。
所以我的模特变成了。。。
公共类项目{公共项目(){
Riebro.RiebroEntities _db = new Riebro.RiebroEntities();
CategoriesList = (from c in _db.Categories
orderby c.Name
select c.Name).ToArray<string>();
}
[Display(Name = "Choose a category")]
public string[] CategoriesList;
}
注意查询末尾的.ToArray,然后突然出现
@Html.DropDownList("Category", new SelectList(Model.CategoriesList))
工作。尽管我要指出,这里的Model关键字似乎是必需的。
在您的视图中,您使用:
@model Models.Project.Project
而在你的控制器动作中,你通过了:
public ActionResult Create()
{
Riebro.Models.Project.Project proj = new Riebro.Models.Project.Project();
return View(proj);
}
注意到区别了吗?CCD_ 5与CCD_。您在控制器上使用的类型似乎与在视图中使用的类型不同。
还要注意,使用包含类名称的名称空间名称是不好的做法。
另一条注释是关于在lambda表达式中使用Model关键字:
@Html.LabelFor(Model => Model.CateogoriesList)
你不应该使用这个关键字。用其他内容替换Model。
看看你的代码,那是什么?这就是导致错误的原因。
<div class="editor-label">@Html.LabelFor( Model => Model.CateogoriesList )</div>
纠正一个
<div class="editor-label">@Html.LabelFor( Model => Model.CategoryId )</div>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true);
<fieldset>
<legend>Submit Your Request</legend>
<div class="editor-label">@Html.LabelFor(x=>x.CategoryId )</div>
<div class="editor-field">
@Html.DropDownList("Category", new SelectList(Model.CateogoriesList) )
</div>
</fieldset>
<p><input type="submit" value="Send for RFP" /></p>
}
这是我对你实体的模拟。我只是添加了另一个用于模拟IQueryable对象的CategoriesList2,但它仍然有效。
public class Project {
public Project() {
CategoryId = 0;
Name = "";
Description = "";
//Categories = new Dictionary<int, string>();
//Entities _db = new Entities(); //ef4
//CateogoriesList = from c in _db.Categories
// orderby c.Name
// select c.Name;
//IQueryable<string> categoriesList = (new string[] { }).AsQueryable();
CateogoriesList = new string[] { "abc", "def", "hij", "klm" };
CategoriesList2 = (new string[] { "abc", "def", "hij", "klm" }).AsQueryable();
}
public int CategoryId { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Project Name")]
public string Name { get; set; }
[Required]
[DataType(DataType.MultilineText)]
[Display(Name = "Project Description")]
public string Description { get; set; }
public string[] CateogoriesList;
public IQueryable<string> CategoriesList2;
}
以下是使用IQueryable类别列表的视图
@model MvcApplication3.Models.Project
@using (Html.BeginForm())
{
@Html.ValidationSummary(true);
<fieldset>
<legend>Submit Your Request</legend>
<div class="editor-label">@Html.LabelFor(x=>x.CategoryId )</div>
<div class="editor-field">
@Html.DropDownList("Category", new SelectList(Model.CategoriesList2) )
</div>
</fieldset>
<p><input type="submit" value="Send for RFP" /></p>
}
您在lambda表达式中使用了保留关键字Model
<div class="editor-label">@Html.LabelFor( Model => Model.CateogoriesList )</div>
试试这个
<div class="editor-label">@Html.LabelFor( m=> m.CateogoriesList )</div>