模型状态无效 [英] Model state not valid

查看:214
本文介绍了模型状态无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个叫做索引视图,规定了所有的线程在我的数据库。然后该视图内我加载上线的所有评论。当我把我的表上是应该建立一个新评论它不断告诉我,我的模型状态无效。它告诉我,它不能从类型字符串转换成键入的资料或意见或标记。本来我有这个作为我的code:

 公众的ActionResult AddComment(螺纹线,绳commentBody)
    {
        如果(ModelState.IsValid)
        {
            _repository.AddComment(螺纹,评论);
            TempData的[消息] =加入您的评论。
            返回RedirectToAction(「指数」);
        }

然后,我把它改成这样:

 公众的ActionResult AddComment(螺纹线,绳commentBody)
    {
        简介配置= _profileRepository.Profiles.FirstOrDefault(X => x.Id == thread.ProfileId);
        标签标签= _tagRepository.Tags.FirstOrDefault(T => t.Id == thread.TagId);
        thread.ThreadTag =标签;
        thread.Profile =轮廓;
        评论评论=新评论()
                              {
                                  CommentBody = commentBody,
                                  ParentThread =螺纹
                              };
        如果(ModelState.IsValid)
        {
            _repository.AddComment(螺纹,评论);
            TempData的[消息] =加入您的评论。
            返回RedirectToAction(「指数」);
        }

这还是告诉我,我的模型状态无效。我如何得到它,这样它不会试图改变国家?

另外这里是被用于调用该操作的形式:

  @using(Html.BeginForm(AddComment,线程,MOD))
            {
                <输入类型=文本名称=AddCommentID =TEXT/>
                <输入类型=提交值=保存/>
            }

在code以上MOD的实例是一个线程模型。
由于在这个要求是线程里面的一切:

 公共线程()
    {
        this.ChildComments =新的HashSet<&评论GT;();
    }    公众诠释标识{搞定;组; }
    公共字符串TopicHeader {搞定;组; }
    公共字符串TopicBody {搞定;组; }
    公众可空< INT> UpVotes {搞定;组; }
    公众可空< INT> DownVotes {搞定;组; }
    公众诠释配置文件ID {搞定;组; }
    公众诠释TAGID {搞定;组; }    公共虚拟档案资料{搞定;组; }
    公共虚拟的ICollection<&评论GT; ChildComments {搞定;组; }
    公共虚拟标签ThreadTag {搞定;组; }

最后评论类:

 公共部分类评论
{
    公众诠释标识{搞定;组; }
    公共字符串CommentBody {搞定;组; }
    公众诠释UpVotes {搞定;组; }
    公众诠释DownVotes {搞定;组; }    公共虚拟线程ParentThread {搞定;组; }
}


解决方案

使用下面的code到错误进行迭代。然后,你可以看到什么领域,什么对象在验证失败。然后你可以去从那里。只看IsValid属性是不会提供足够的信息。

  VAR误差= ModelState.Values​​.SelectMany(V => v.Errors);

然后再通过错误循环。

So I have a view called index that lays out all of the threads in my database. Then inside that view I'm loading all the comments on the threads. When I call on my form that is supposed to create a new comment it keeps telling me that my model state is invalid. It tells me that it cannot convert from type string to type profile or comment or tag. Originally I had this as my code:

 public ActionResult AddComment(Thread thread, string commentBody)
    {
        if (ModelState.IsValid)
        {
            _repository.AddComment(thread, comment);
            TempData["Message"] = "Your comment was added.";
            return RedirectToAction("Index");
        }

Then I changed it to this:

 public ActionResult AddComment(Thread thread, string commentBody)
    {
        Profile profile = _profileRepository.Profiles.FirstOrDefault(x => x.Id ==       thread.ProfileId);
        Tag tag = _tagRepository.Tags.FirstOrDefault(t => t.Id == thread.TagId);
        thread.ThreadTag = tag;
        thread.Profile = profile;
        Comment comment = new Comment()
                              {
                                  CommentBody = commentBody,
                                  ParentThread = thread
                              };
        if (ModelState.IsValid)
        {
            _repository.AddComment(thread, comment);
            TempData["Message"] = "Your comment was added.";
            return RedirectToAction("Index");
        }

This still tells me that my model state is invalid. How do I get it so that it will not try to change the state?

Also here is the form that is being used to call this action:

@using(Html.BeginForm("AddComment", "Thread", mod))
            {
                <input type="text" name="AddComment" id="text" />
                <input type="submit" value="Save"/>
            }

In the instance of code above mod is the model which is a thread. And as requested here is everything inside of thread:

 public Thread()
    {
        this.ChildComments = new HashSet<Comment>();
    }

    public int Id { get; set; }
    public string TopicHeader { get; set; }
    public string TopicBody { get; set; }
    public Nullable<int> UpVotes { get; set; }
    public Nullable<int> DownVotes { get; set; }
    public int ProfileId { get; set; }
    public int TagId { get; set; }

    public virtual Profile Profile { get; set; }
    public virtual ICollection<Comment> ChildComments { get; set; }
    public virtual Tag ThreadTag { get; set; }

And finally the comment class:

 public partial class Comment
{
    public int Id { get; set; }
    public string CommentBody { get; set; }
    public int UpVotes { get; set; }
    public int DownVotes { get; set; }

    public virtual Thread ParentThread { get; set; }
}

解决方案

Use the code below to iterate through the errors. Then you can see what field and what object is failing on validation. And then you can go from there. Just looking at the IsValid property is not going to give enough information.

var errors = ModelState.Values.SelectMany(v => v.Errors);

And then loop through the errors.

这篇关于模型状态无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆