C# 在MVC5中设置默认的DropDownList值
这是我第一次发帖,我对编码和MVC完全是个初学者 我正在尝试创建一个过滤系统来过滤用户的帖子。我的逻辑是允许用户按用户名和类别名称搜索关键字。用户可以使用关键字和/或用户名和/或类别名称。关键字是一个简单的文本框,用户名和类别是下拉列表。 当我只使用关键字TextBoxFor和一个DropDownList运行代码时,一切正常,但是当我使用另一个DropDownList时,我会得到这个错误 具有键“UserID”的ViewData项的类型为“System.String”,但必须为“IEnumerable”类型 我做了一些研究,得出结论,UserID DropDownList给出了一个空值。我唯一能想到的就是给UserID一个默认值,但是我无法做到这一点。有没有更好的方法来代替给它一个默认值?如果没有,我如何才能做到这一点 谢谢, bobdbuider 我的视图模型:C# 在MVC5中设置默认的DropDownList值,c#,asp.net,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc 5,这是我第一次发帖,我对编码和MVC完全是个初学者 我正在尝试创建一个过滤系统来过滤用户的帖子。我的逻辑是允许用户按用户名和类别名称搜索关键字。用户可以使用关键字和/或用户名和/或类别名称。关键字是一个简单的文本框,用户名和类别是下拉列表。 当我只使用关键字TextBoxFor和一个DropDownList运行代码时,一切正常,但是当我使用另一个DropDownList时,我会得到这个错误 具有键“UserID”的ViewData项的类型为“System.String”,但必须为“IEnumera
public class DTOSearchModel
{
#region Properties
public string Keyword { get; set; }
[Display(Name = "Username")]
public string UserID { get; set; }
[Display(Name = "Category Name")]
public int CategoryID { get; set; }
public List<Models.PostTable> PostResults { get; set; }
#endregion
public DTOSearchModel()
{
Keyword = "";
UserID = "Select a Username";
CategoryID = 0;
PostResults = new List<Models.PostTable>();
}
}
}
//POST Search
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(DTOSearchModel model)
{
if (ModelState.IsValid)
{
var postTables = db.PostTables.Include(p => p.AspNetUser).Include(p => p.CategoryTable).Include(p => p.StatusTable);
var posts = postTables.ToList();
//CategoryID
if (model.CategoryID !=0)
{
ViewBag.CategoryID = new SelectList(db.CategoryTables, "CategoryID", "CategoryName");
posts = posts.Where(k => k.CategoryID.Equals(model.CategoryID)).ToList();
}
//UserID
ViewBag.UserID = new SelectList(db.AspNetUsers, "Id", "Email" ,"Select");
posts = posts.Where(k => k.UserID.Equals(model.UserID)).ToList();
//Keyword
if (!String.IsNullOrEmpty(model.Keyword))
{
posts = posts.Where(k => k.Title.ToLower().Contains(model.Keyword.ToLower()) || k.Body.ToLower().Contains(model.Keyword.ToLower())).ToList();
}
model.PostResults = posts;
}
return View(model);
}
}
}
<div class="col-md-3">
@Html.LabelFor(model => model.Keyword)
@Html.TextBoxFor(model => model.Keyword, htmlAttributes: new { @class = "form-control" })
</div>
<div class="col-md-4">
@Html.LabelFor(model => model.UserID)
@Html.DropDownList("UserID", null, htmlAttributes: new { @class = "form-control" })
</div>
<div class="col-md-4">
@Html.LabelFor(model => model.CategoryID)
@Html.DropDownList("CategoryID" , null, "Select a Category Name", htmlAttributes: new { @class = "form-control" })
</div>
我的观点:
public class DTOSearchModel
{
#region Properties
public string Keyword { get; set; }
[Display(Name = "Username")]
public string UserID { get; set; }
[Display(Name = "Category Name")]
public int CategoryID { get; set; }
public List<Models.PostTable> PostResults { get; set; }
#endregion
public DTOSearchModel()
{
Keyword = "";
UserID = "Select a Username";
CategoryID = 0;
PostResults = new List<Models.PostTable>();
}
}
}
//POST Search
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(DTOSearchModel model)
{
if (ModelState.IsValid)
{
var postTables = db.PostTables.Include(p => p.AspNetUser).Include(p => p.CategoryTable).Include(p => p.StatusTable);
var posts = postTables.ToList();
//CategoryID
if (model.CategoryID !=0)
{
ViewBag.CategoryID = new SelectList(db.CategoryTables, "CategoryID", "CategoryName");
posts = posts.Where(k => k.CategoryID.Equals(model.CategoryID)).ToList();
}
//UserID
ViewBag.UserID = new SelectList(db.AspNetUsers, "Id", "Email" ,"Select");
posts = posts.Where(k => k.UserID.Equals(model.UserID)).ToList();
//Keyword
if (!String.IsNullOrEmpty(model.Keyword))
{
posts = posts.Where(k => k.Title.ToLower().Contains(model.Keyword.ToLower()) || k.Body.ToLower().Contains(model.Keyword.ToLower())).ToList();
}
model.PostResults = posts;
}
return View(model);
}
}
}
<div class="col-md-3">
@Html.LabelFor(model => model.Keyword)
@Html.TextBoxFor(model => model.Keyword, htmlAttributes: new { @class = "form-control" })
</div>
<div class="col-md-4">
@Html.LabelFor(model => model.UserID)
@Html.DropDownList("UserID", null, htmlAttributes: new { @class = "form-control" })
</div>
<div class="col-md-4">
@Html.LabelFor(model => model.CategoryID)
@Html.DropDownList("CategoryID" , null, "Select a Category Name", htmlAttributes: new { @class = "form-control" })
</div>
@LabelFor(model=>model.Keyword)
@TextBoxFor(model=>model.Keyword,htmlAttributes:new{@class=“form control”})
@LabelFor(model=>model.UserID)
@DropDownList(“UserID”,null,htmlAttributes:new{@class=“formcontrol”})
@LabelFor(model=>model.CategoryID)
@DropDownList(“CategoryID”,null,“选择类别名称”,htmlAttributes:new{@class=“form control”})
您不应在视图包中使用与您的模型相同的属性名称。在您的模型中有一个UserID
属性,类型为string
,在ViewBag
中还有一个UserID
类型为SelectList
。您的视图使用的是模型中的类型为string
,但DropDownList
需要IEnumerable
的视图,因此出现以下错误:
具有键“UserID”的ViewData项的类型为“System.String”,但必须为“IEnumerable”类型
您应该尝试不要使用ViewBag
,而是为您的视图创建一个模型,该模型包含视图所需的所有内容,然后在视图中使用它。有关更多详细信息,请参阅答案。感谢您快速而详细的回答。我已将视图模型中的UserID更改为Username,因此它们将有所不同。我还尝试在视图模型中更改CategoryID,但它给了我一个错误,所以我将其保留为CategoryID。现在的问题是,我总是得到0篇与用户名和/或类别匹配的帖子。我还单独测试了用户过滤器,但它不能正常工作,所以问题在于我对Viewmodel所做的更改。你看到我提到的链接中的代码了吗?有一把小提琴,它展示了如何创建下拉列表。请仔细研究这段代码,它将帮助您解决您遇到的问题。是的,我已经看了小提琴。我只是问这个问题,因为有人告诉我不是这样的。自己给出值而不是让ViewBag从数据库获取值,这不是一种糟糕的做法吗?例如:如果我想添加类别或用户名,我需要自己在newSelectListItem中添加条目。不,您可以在模型中创建IEnumerable或SelectList类型的属性,然后从数据库中填充它。然后在视图中使用该属性。看见