我正在创建新闻页面。我有新闻模型,新闻有一个类别。类别来自数据库。我希望能够创建新闻并为新闻分配类别。以下是我尝试执行的操作。问题是我试图为类别创建一个下拉列表,一旦填写表格,它将被提交以保存在数据库中。
这是错误:我正在传递IEnumerable<SelectListItem> categoriesList
进行查看,但是它正在等待新闻模型。如何在一个视图中使用多个模型?我如何解决下面的代码,使其可以工作?
@model App.Models.News
@{
ViewBag.Title = "Create";
}
<h2>Create news</h2>
@using (Html.BeginForm()) {
<div>
<fieldset>
<legend>Category Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.Title)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Title)
@Html.ValidationMessageFor(m => m.Title)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Category)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Category)
@Html.ValidationMessageFor(m => m.Category)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.NewsContent)
</div>
<div class="editor-field">
@Html.TextAreaFor(m => m.NewsContent)
@Html.ValidationMessageFor(m => m.NewsContent)
</div>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
}
public ActionResult Create()
{
IList<Category> categories;
using (var session = NHibernateHelper.OpenSession())
{
using (var tx = session.BeginTransaction())
{
categories = session.CreateCriteria(typeof(Category)).List<Category>();
tx.Commit();
}
}
IEnumerable<SelectListItem> categoriesList = categories.Select(category => new SelectListItem() { Text = category.Name, Value = category.Id.ToString() });
return View(categoriesList);
}
最佳答案
如果您的视图需要News对象,则必须将News对象传递给它。如果要使用多个模型,则可以创建一个视图模型:
public class NewsViewModel
{
public string SelectedCategoryId { get; set; }
public IEnumerable<SelectListItem> Categories { get; set; }
... put any properties that your view might require
}
然后将您的视图强烈键入
NewsViewModel
,并让控制器操作将其实例传递给该视图。您应该将News实例传递给视图:
public ActionResult Create()
{
IList<Category> categories;
using (var session = NHibernateHelper.OpenSession())
using (var tx = session.BeginTransaction())
{
categories = session.CreateCriteria(typeof(Category)).List<Category>();
tx.Commit();
}
IEnumerable<SelectListItem> categoriesList = categories.Select(category => new SelectListItem() { Text = category.Name, Value = category.Id.ToString() });
var news = new NewsViewModel
{
Categories = categoriesList
};
return View(news);
}
并且在您看来:
@model App.Models.NewsViewModel
另一种可能性(我不推荐)是使用
ViewBag
:public ActionResult Create()
{
IList<Category> categories;
using (var session = NHibernateHelper.OpenSession())
using (var tx = session.BeginTransaction())
{
categories = session.CreateCriteria(typeof(Category)).List<Category>();
tx.Commit();
}
IEnumerable<SelectListItem> categoriesList = categories.Select(category => new SelectListItem() { Text = category.Name, Value = category.Id.ToString() });
ViewBag.Categories = categoriesList;
// or fetch from DB or whatever
var news = new News();
return View(news);
}
并在视图中:
@model App.Models.News
@{
ViewBag.Title = "Create";
}
<h2>Create news</h2>
@using (Html.BeginForm()) {
<div>
<fieldset>
<legend>Category Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.Title)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Title)
@Html.ValidationMessageFor(m => m.Title)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Category)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Category)
@Html.ValidationMessageFor(m => m.Category)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.NewsContent)
</div>
<div class="editor-field">
@Html.TextAreaFor(m => m.NewsContent)
@Html.ValidationMessageFor(m => m.NewsContent)
</div>
<div class="editor-label">
@Html.Label("SelectedCatgoryId")
</div>
<div class="editor-field">
@Html.DropDownList("SelectedCatgoryId", (IEnumerable<SelectListItem>)ViewBag.Categories)
@Html.ValidationMessage("SelectedCatgoryId")
</div>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
}
关于c# - Razor 呈现新闻和类别,多种模型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9886991/