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

标签 c# asp.net-mvc asp.net-mvc-3 razor

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

这是错误:我正在传递 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/

相关文章:

c# - 线程安全数据访问对象 C#

c# - 使用 PdfSharp-WP7 在 wp8 中读取和写入带有一些图像的 pdf 文件

c# - 在 C# razor 中使用@event

asp.net - 当文件位于不同的服务器上而不先将其下载到我的服务器上时,使用 ASP.Net MVC 在浏览器中强制下载文件

jquery - 在 MVC 中将数据从 Jquery 加载到表中

c# - foreach 语句不能对变量进行操作 MVC3 错误

c# - 在单个数据表中合并数据集表

javascript - 如何在运行时在knockoutjs对象中添加新的计算属性?

c# - 如何在 MVC 中为具有多个属性的 View 模型添加验证错误?

asp.net-mvc - OutputCache 停止工作