MVC3 - 插入使用视图模型 - 对象引用不设置到对象的实例 [英] MVC3 - Insert using ViewModel - Object reference not set to an instance of an object

查看:155
本文介绍了MVC3 - 插入使用视图模型 - 对象引用不设置到对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个型号,如下图所示,并正尝试从一个视图中插入每一个数据库中。我曾经试图做到这一点创建一个视图模型。

 公共类博客
{
    公众诠释BlogID {搞定;组; }
    公共字符串名称{搞定;组; }
    公众的DateTime CreatedOn {搞定;组; }    公共虚拟用户用户{搞定;组; }
    公共虚拟的ICollection<&博文GT;帖子{搞定;组; }
}公共类博文
{
    公众诠释{帖子ID获取;组; }
    公共字符串车身{搞定;组; }
    公众的DateTime CreatedOn {搞定;组; }    公众诠释用户名{搞定;组; }
    公共虚拟用户用户{搞定;组; }
}公共类BlogViewModel
{
    公共博客博客{搞定;组; }
    公开博文的博文{搞定;组; }
}

使用我张贴到创建控制器视图模型:

  [HttpPost]
    公众的ActionResult创建(BlogViewModel博客)
    {
        如果(ModelState.IsValid)
        {
            用户的用户= unit.UserRepository.GetUser();
            blog.Blog.User =用户;
            blog.Blog.CreatedOn = DateTime.Now;            unit.BlogRepository.Insert(blog.Blog);
            unit.BlogPostRepository.Insert(blog.BlogPost);
            unit.Save();            返回RedirectToAction(「指数」);
        }        返回查看(博客);
    }

这保持引发错误


  

对象引用未设置到对象的实例。


就行了 blog.Blog.User =用户

这是我在做什么任何想法错了吗?

修改
我查了POST数据,它是所有那里,正确的,但它被张贴的一切, Blog.Title = BlogPost.Body = 所以在控制器中的视图模型并没有收到任何东西。如果我控制器的ActionResult更改为:

公众的ActionResult创建(博客博客,博文后)

然后,它所有的作品。那么,为什么没有出现在视图模型格式发送的数据?我使用你的意见和之间的显式视图模型基于交互控制器

  @model Test.Models.BlogViewModel
@using(Html.BeginForm())
{
    @ Html.ValidationSummary(真)
    <&字段集GT;
        <传奇>博客< /传说>
        < D​​IV CLASS =编辑标记>
            @ Html.LabelFor(型号=> model.Blog.Title)
        < / DIV>
        < D​​IV CLASS =主编场>
            @ Html.EditorFor(型号=> model.Blog.Title)
            @ Html.ValidationMessageFor(型号=> model.Blog.Title)
        < / DIV>
        < D​​IV CLASS =编辑标记>
            @ Html.LabelFor(型号=> model.BlogPost.Body)
        < / DIV>
        < D​​IV CLASS =主编场>
            @ Html.EditorFor(型号=> model.BlogPost.Body)
            @ Html.ValidationMessageFor(型号=> model.BlogPost.Body)
        < / DIV>
        &所述p为H.;
            <输入类型=提交值=创建/>
        &所述; / P>
    < /字段集>
}
< D​​IV>
    @ Html.ActionLink(返回目录,索引)
< / DIV>


解决方案

只需重命名你的行动的说法:

 公众的ActionResult创建(BlogViewModel视图模型)

有一个冲突,因为你的行动的说法叫做博客,但您的视图模型( BlogViewModel )具有物业名为博客。问题是,在默认模型粘合剂不再知道在这样的情况下做的。

哦,如果你绝对需要有一个名为您的操作参数博客,那么你也可以重命名博客属性在你的视图模型。

I have got two models, seen below, and am trying to insert one of each to the database from one view. I have created a view model in an attempt to do this.

public class Blog
{
    public int BlogID { get; set; }
    public string Title { get; set; }
    public DateTime CreatedOn { get; set; }

    public virtual User User { get; set; }
    public virtual ICollection<BlogPost> Posts { get; set; }     
}

public class BlogPost
{
    public int PostID { get; set; }
    public string Body { get; set; }
    public DateTime CreatedOn { get; set; }

    public int UserID { get; set; }
    public virtual User User { get; set; }
}

public class BlogViewModel
{
    public Blog Blog { get; set; }
    public BlogPost BlogPost { get; set; }
}

Using the view model i am posting to the create controller:

[HttpPost]
    public ActionResult Create(BlogViewModel blog)
    {
        if (ModelState.IsValid)
        {
            User user = unit.UserRepository.GetUser();
            blog.Blog.User = user;
            blog.Blog.CreatedOn = DateTime.Now;

            unit.BlogRepository.Insert(blog.Blog);
            unit.BlogPostRepository.Insert(blog.BlogPost);
            unit.Save();

            return RedirectToAction("Index");  
        }

        return View(blog);
    }

This keep throwing the error

Object reference not set to an instance of an object.

on the line blog.Blog.User = user.

Any thoughts on what i'm doing wrong?

EDIT I checked the POST data and it was all there and correct but it was posting everything as Blog.Title = and BlogPost.Body = so the viewmodel in the controller wasn't receiving anything. If i change the controller actionresult to:

public ActionResult Create(Blog blog, BlogPost post)

Then it all works. So why doesn't data sent in the viewmodel format? I am using explicit view model based interaction between your views and controller

@model Test.Models.BlogViewModel
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Blog</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Blog.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Blog.Title)
            @Html.ValidationMessageFor(model => model.Blog.Title)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.BlogPost.Body)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.BlogPost.Body)
            @Html.ValidationMessageFor(model => model.BlogPost.Body)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

解决方案

Simply rename your action argument:

public ActionResult Create(BlogViewModel viewModel)

There is a conflict because your action argument is called blog and yet your view model (BlogViewModel) has a property called Blog. The problem is that the default model binder no longer knows what to do in such situation.

Oh and if you absolutely need to have your action argument called blog then you could also rename the Blog property on your view model.

这篇关于MVC3 - 插入使用视图模型 - 对象引用不设置到对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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