MVC4:[HttpPost]上的ViewModel为空

时间:2013-01-28 14:33:54

标签: asp.net-mvc-4 http-post viewmodel

我正在尝试在MVC4中创建一个简单的新闻系统。我是新手,我有一个简单的新闻基类,如下所示:

NewsPost:

public class NewsPost
{
    public virtual int Id { get; set; }
    public virtual string Subject { get; set; }
    public virtual string Content { get; set; }
}

然后将其附加到类别类:

新闻分类

public class NewsCategory
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual ICollection<NewsPost> NewsPosts { get; set; }
}

然后我有一个控制器来创建这些NewsPosts:

NewsController

private readonly INewMvcSiteDataSource _db;

public NewsController(INewMvcSiteDataSource db)
{
    _db = db;
}

[HttpGet]
public ActionResult Create()
{
    var model = new CreateNewsViewModel();

    model.Categories = new SelectList(_db.NewsCategories, "Id", "Name");

    return View(model);
}

[HttpPost]
public ActionResult Create(CreateNewsViewModel newsModel)
{
    if (ModelState.IsValid)
    {
        int id = int.Parse(newsModel.SelectedCategories.Single(f => f.Selected).Value);
    }
    return View(newsModel);
}

最后,为了方便创建NewsPost,我使用CreateNewsViewModel,如下所示:

CreateNewsViewModel

public class CreateNewsViewModel
{
    [Required]
    public string Subject { get; set; }

    [Required]
    public string Content { get; set; }

    public SelectList Categories { get; set; }
    public SelectList SelectedCategories { get; set; }
}

我的观点如下:

Create.cshtml

@model NewMvcSite.Models.CreateNewsViewModel

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>CreateNewsViewModel</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Subject)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Subject)
            @Html.ValidationMessageFor(model => model.Subject)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Content)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Content)
            @Html.ValidationMessageFor(model => model.Content)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Categories)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.Categories, Model.SelectedCategories)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

出于某种原因,当我单击提交按钮来创建新闻帖时,我的viewmodel不会被返回,并且由于[HttpPost] Create操作没有无参数构造函数,因此“没有为无参数构造函数定义这个对象。“

我一直在尝试通过其他帖子说明同样的问题,但我没有看到他们正在做什么和我在做什么之间的联系。我希望有人可以帮助我。

谢谢。

3 个答案:

答案 0 :(得分:1)

如果要从下拉列表中选择类别,首先应将属性添加到模型中以保存所选类别的ID。像这样的东西(属性类别):

public class CreateNewsViewModel
    {
        [Required]
        public string Subject { get; set; }

        [Required]
        public string Content { get; set; }

        public SelectList Categories { get; set; }
        public SelectList SelectedCategories { get; set; }
        public int Category { get; set; }
    }

public class CreateNewsViewModel { [Required] public string Subject { get; set; } [Required] public string Content { get; set; } public SelectList Categories { get; set; } public SelectList SelectedCategories { get; set; } public int Category { get; set; } }

之后,您应该将代码更改为 model.Category

model.Categories = new SelectList(categories, "Id", "Name", model.Category);
而且,在您看来,类别的编辑器应该如下所示:

model.Categories = new SelectList(categories, "Id", "Name", model.Category);

答案 1 :(得分:0)

model.Categories = new SelectList(_db.NewsCategories, "Id", "Name");

在此行填写Categories属性

@Html.DropDownListFor(model => model.Categories, Model.SelectedCategories)

在此尝试使用select名称和Categories项目

创建SelectedCategories
int id = int.Parse(newsModel.SelectedCategories.Single(f => f.Selected).Value);

在这里尝试获取所选项目,但在表单上提交您只发送一个号码,而不是列表。您SelectList之一必须是int

答案 2 :(得分:0)

我找到了解决方案。

我不得不将ViewModel更改为:

public IEnumerable<SelectListItem> Categories { get; set; }
public int SelectedCategoryId { get; set; }

我将控制器更改为:

var model = new CreateNewsViewModel
                {
                    Categories = new SelectList(_db.NewsCategories, "Id", "Name")
                };

并将视图更改为:

@Html.DropDownListFor(x => x.SelectedCategoryId, Model.Categories)