ASP.NET MVC 对象引用错误
ASP.NET MVC Object reference error
我有一个非常简单的入门 .NET 应用程序,在我的控制器中使用此方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SampleMVC.Models;
namespace SampleMVC
{
public class TurtleController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
return View();
}
public IActionResult Detail( int id ) {
var turtle = new TurtleModel{ id = 1, title = "Pink Turtle" };
return View("Detail",turtle);
}
}
我的 TurtleModel
看起来像这样:
using System;
namespace SampleMVC.Models
{
public class TurtleModel
{
public int id { get; set; }
public string title { get; set; }
}
}
我的观点是这样的:
@page
@model SampleMVC.Models.TurtleModel;
@{
<p>tester @Model.title</p>
}
Startup.cs(路线)
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
但是,我在尝试查看 URL /Turtle/Detail/1
时遇到了空指针/对象引用错误。想知道你是否能指出我正确的方向?我已经调试了 Controller 并且 turtleModel 肯定被实例化了,所以我不明白为什么视图在传递给它之后认为它不是。
问题出现在视图中:
@page
@model SampleMVC.Models.TurtleModel;
@{
<p>tester @Model.title</p>
}
改为
@model SampleMVC.Models.TurtleModel;
@{
<p>tester @Model.title</p>
}
我有一个非常简单的入门 .NET 应用程序,在我的控制器中使用此方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SampleMVC.Models;
namespace SampleMVC
{
public class TurtleController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
return View();
}
public IActionResult Detail( int id ) {
var turtle = new TurtleModel{ id = 1, title = "Pink Turtle" };
return View("Detail",turtle);
}
}
我的 TurtleModel
看起来像这样:
using System;
namespace SampleMVC.Models
{
public class TurtleModel
{
public int id { get; set; }
public string title { get; set; }
}
}
我的观点是这样的:
@page
@model SampleMVC.Models.TurtleModel;
@{
<p>tester @Model.title</p>
}
Startup.cs(路线)
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
但是,我在尝试查看 URL /Turtle/Detail/1
时遇到了空指针/对象引用错误。想知道你是否能指出我正确的方向?我已经调试了 Controller 并且 turtleModel 肯定被实例化了,所以我不明白为什么视图在传递给它之后认为它不是。
问题出现在视图中:
@page
@model SampleMVC.Models.TurtleModel;
@{
<p>tester @Model.title</p>
}
改为
@model SampleMVC.Models.TurtleModel;
@{
<p>tester @Model.title</p>
}