在MVC中将数据获取到ViewModel中

时间:2019-09-20 20:09:41

标签: asp.net-mvc

这是我第一次尝试MVC实现,但是我一直试图解决对象引用错误。数据没有从模型传递到ViewModel,我也不知道为什么。我试图将数据传递到主页(索引)页面,但得到“对象引用未设置为对象的实例”。错误,因为未填充@model。我不知道“链”在哪里断裂。我试图在下面添加相关的代码。

模型是Model / blogpost.cs,EF则创建了相关表。我添加了一行数据进行测试。

 public class BlogPosts
{
    public int Id { get; set; }
    [Required]
    public string Title { get; set; }
    [Required]
    public string ShortPost { get; set; }
    [Required]
    public string Post { get; set; }
    [Required]
    public string Tags { get; set; }
    [Required]
    public DateTime Updated { get; set; }
}

控制器是Controller / HomeController.cs,

        public ActionResult Index()
    {
        var viewModel = new IndexViewModel();
        return View("Index",viewModel);
    }

视图模型是View / IndexViewModel.cs

 public class IndexViewModel
{
    public BlogPosts Blog { get; set; }
}

,由View / Index.cshtml使用。这是HTML的代码段,从底部开始的第五行是@model的第一次使用,它是NULL对象:

<!DOCTYPE html>
@model congresssucks_asp.ViewModels.IndexViewModel
@{
ViewBag.Title = "BlogPosts";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" type="text/css" href="~/Content/normalize.css" />
    <link rel="stylesheet" type="text/css" href="~/Content/grid.css" />
    <link rel="stylesheet" type="text/css" href="~/Content/style.css" />
    <link rel="stylesheet" type="text/css" href="~/Content/queries.css" />
    <link href="https://fonts.googleapis.com/css?family=Lato:100,300,300i,400" rel="stylesheet">
    <meta charset="utf-8" />
    <title>Congress Sucks!</title>
</head>
<body>
    <section class="section-blog js-section-blog" id="latest">
        <div class="row">
            <h2>Most Recent Posts</h2>
        </div>
        <div class="row">
            <div class="col span-1-of-4 box">
                <div class="blog-box">
                    <h3>@Model.Blog.Title </h3>
                    <div class="blog-date">@Model.Blog.Updated</div>
                    <p>
                        @Model.Blog.ShortPost
                    </p>

它导致此错误:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 25:             <div class="col span-1-of-4 box">
Line 26:                 <div class="blog-box">
Line 27:                     <h3>@Model.Blog.Title </h3>
Line 28:                     <div class="blog-date">@Model.Blog.Updated</div>
Line 29:                     <p>

我在控制器上放置了一个断点,并且viewmodel对象似乎为空。

Breakpoint

2 个答案:

答案 0 :(得分:2)

#Model class
public class BlogPosts
{
    public int Id { get; set; }
    [Required]
    public string Title { get; set; }
    [Required]
    public string ShortPost { get; set; }
    [Required]
    public string Post { get; set; }
    [Required]
    public string Tags { get; set; }
    [Required]
    public DateTime Updated { get; set; }
}

#the controller is Controller/HomeController.cs
public ActionResult Index()
{
    var viewModel = new IndexViewModel();
    viewModel.Blog = new  BlogPosts();
    return View("Index",viewModel);
}

#the view model is View/IndexViewModel.cs
public class IndexViewModel
{
    public BlogPosts Blog { get; set; }
}

答案 1 :(得分:0)

您仅初始化IndexViewModel模型,但未初始化 BlogPosts ,请参见下面的代码示例,这将对您有所帮助。

public ActionResult Index()
{
    var viewModel = new IndexViewModel();
    //viewModel.Blog <--- This object is null
    viewModel.Blog = new BlogPosts(); //Get data from database or initialize an empty object(Ex: viewModel.Blog = GetPostData())
    //viewModel.Blog <--- Then this object initialize as an empty object

    return View("Index",viewModel);
}
相关问题