具有键&#39; CategoryId&#39;的ViewData项。属于&#39; System.Int32&#39;但必须是&#39; IEnumerable <selectlistitem>&#39; </selectlistitem>

时间:2014-12-07 18:42:30

标签: c# asp.net asp.net-mvc-4

我试图将一些新帖子保存到我的数据库中,但我反复收到错误..我有一个班级&#34; Posts.cs&#34;以下

    public partial class Post
    {
        public Post()
        {
            this.Replies = new HashSet<Reply>();
        }

        public int PostId { get; set; }
        public string PostTitle { get; set; }
        public string PostContent { get; set; }
        public int AuthorId { get; set; }
        public int CategoryId { get; set; }
        public System.DateTime DateCreated { get; set; }

        public virtual Category Category { get; set; }
        public virtual UserProfile UserProfile { get; set; }
        public virtual ICollection<Reply> Replies { get; set; }
    }
}

和一个ViewModel,我用它来呈现视图供用户添加帖子

 public class AddPostsVM
    {
        [Display(Name = "CategoryId")]
        public int CategoryId { get; set; }

        [Required()]
        [Display(Name="Title")]
        public string PostTitle { get; set; }

        [Required()]
        [Display(Name="Content")]
        public string PostContent { get; set; }

        [Required()]
        [Display(Name="Select a Category")]
        public string CategoryName { get; set; }

        public List<SelectListItem>CategoriesList { get; set; }
    }

我使用视图模型在视图中使用下拉列表呈现视图,以获取数据库中的类别列表,这样当用户添加帖子时,他们可以选择他们希望帖子属于的类别

[HttpGet]
    public ActionResult CreatePost()
    {
        AddPostsVM model = new AddPostsVM();
        var categories = (from c in db.Categories
                         select new
                         {
                             CategoryName = c.CategoryName,
                             CategoryId = c.CategoryId
                         }).ToList();

        model.CategoriesList = categories.Select(c => new SelectListItem
        {
            Text = c.CategoryName,
            Value = c.CategoryId.ToString()

        }).ToList();


        return View(model);
    }

在我的视图中CreatePost.cshtml我有这个

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.PostTitle, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PostTitle, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PostTitle, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PostContent, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @*@Html.TextAreaFor(model => model.PostContent, new { htmlAttributes = new { @class = "form-control" } })*@
                <textarea class="form-control" name="PostContent" id="PostContent" rows="3"></textarea>
                @Html.ValidationMessageFor(model => model.PostContent, "", new { @class = "text-danger" })
            </div>
        </div>
                <div class="form-group">
            @Html.LabelFor(model => model.CategoryId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.CategoryId, Model.CategoriesList, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
            </div>
        </div>


        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

每当我尝试使用下面的帖子操作

将新帖子添加到数据库时
[HttpPost]
public ActionResult CreatePost(AddPostsVM model)
{
    try
    {
        if (ModelState.IsValid)
        {
            var newPost = db.Posts.Create();
            newPost.PostTitle = model.PostTitle;
            newPost.PostContent = model.PostContent;
            FormsIdentity identity = (FormsIdentity)User.Identity;
            int nUserID = Int32.Parse(identity.Ticket.UserData);
            newPost.AuthorId = nUserID;
            newPost.CategoryId = model.CategoryId;
            newPost.DateCreated = DateTime.Now;
            db.Posts.Add(newPost);
            db.SaveChanges();
            return RedirectToAction("Index", "Posts");
        }
        else
        {
            ModelState.AddModelError("", "Invalid Model");

        }

    }
    catch (DbEntityValidationException e)
    {
        foreach (var eve in e.EntityValidationErrors)
        {
            Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                 eve.Entry.Entity.GetType().Name, eve.Entry.State);
            foreach (var ve in eve.ValidationErrors)
            {
                Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                    ve.PropertyName, ve.ErrorMessage);
            }
        }
        throw;


    }
    return View(model);
}

我可以看到ModelState.IsValid返回false并且我收到了错误

The ViewData item that has the key 'CategoryId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

ModelState始终返回false的原因是属性CategoryName具有[Required]属性,并且您不呈现控件并回发值,因此它始终为null因此无效。但是CategoryName与您选择的与您下拉列表相关联的类别相关,因此您应该删除此属性,只需依赖绑定到下拉列表的CategoryId

出现错误的原因是当您返回视图时(由于上述问题而一直在发生),您不会重新分配CategoriesList的值,因此其null和{{1抛出异常。

为避免重复,您可以将常用代码重新分解为私有方法(请注意,这假设您将@Html.DropDownListFor(model => model.CategoryId, Model.CategoriesList, ..)更改为public List<SelectListItem> CategoriesList { get; set; }

public SelectList CategoriesList { get; set; }

您现在可以在GET方法和POST方法中调用

private void ConfigureEditModel(AddPostsVM model)
{
  model.CategoriesList = new SelectList(db.Categories, "CategoryID", "CategoryName");
  // any other common stuff
}