繁体   English   中英

你调用的对象是空的。 MVC

[英]object reference not set to an instance of an object. MVC

我不知道为什么它是null但我在控制器中传递了一个值但它没有将它传递给视图

看法:

@model LABEX7_GUINTUJOSHUA.Models.DataModel1
@{
   ViewBag.Title = "HomePage";
   Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>@Model.name</h2>

控制器:

using System.Web.Mvc;
using LABEX7_GUINTUJOSHUA.Models;

namespace LABEX7_GUINTUJOSHUA.Controllers
{
    public class DataModel1Controller : Controller
    {
        // GET: DataModel1
        public ActionResult Index()
        {
            DataModel1 user = new DataModel1()
            {
                name = "Joshua Guintu"
            };


            return View(user);
        }
        public ActionResult HomePage()
        {


            return View();
        }
    }

}

模型:

namespace LABEX7_GUINTUJOSHUA.Models
{
    public class DataModel1
    {
        public String name { get; set; }
    }

请检查此链接中的错误ERORR PIC

由于您正在访问HomePage ,您需要创建一个对象实例,将模型传递给HomePage ,而不仅仅是 Index 。

public ActionResult Index()
{
   DataModel1 user = new DataModel1()
   {
      name = "Joshua Guintu"
   };
   return View(user);
}

public ActionResult HomePage()
{
   DataModel1 user = new DataModel1()
   {
      name = "Joshua Guintu"
   };
   return View(user);
}

您正在访问主页主页,在主页视图中您正在访问模型(DataModel1)和主页操作方法,您还没有将模型作为参数传递

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM