gpt4 book ai didi

c# - 用于呈现新闻和类别的 Razor ,多个模型

转载 作者:太空宇宙 更新时间:2023-11-03 22:01:37 25 4
gpt4 key购买 nike

我正在创建一个新闻页面。我有新闻模型,新闻有一个类别。类别来自数据库。我希望能够创建新闻并为新闻分配类别。以下是我尝试这样做的方法。问题是我正在尝试为类别创建一个下拉列表,一旦表单填写完毕,它将被提交并保存在数据库中。

这是错误:我正在传递 IEnumerable<SelectListItem> categoriesList查看,但它期待新闻模型。如何在一个 View 中使用多个模型?我怎样才能修复下面的代码以使其正常工作?

@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);
}

最佳答案

如果您的 View 需要一个新闻对象,您必须将一个新闻对象传递给它。如果你想使用多个模型,你可以创建一个 View 模型:

public class NewsViewModel
{
public string SelectedCategoryId { get; set; }
public IEnumerable<SelectListItem> Categories { get; set; }

... put any properties that your view might require
}

然后将您的 View 强类型化为 NewsViewModel 并让您的 Controller 操作将其实例传递给 View 。

您应该将新闻实例传递给您的 View :

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);
}

在 View 中:

@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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com