我正在阅读一些ASP.Net核心教程。我正在使用模型。我创建了一个Quote模型,将名称和一些文本存储到数据库中,但出现错误:
obj / Debug / netcoreapp2.1 / Razor / Views / Home / CreateQuote.g.cshtml.cs(23,92):错误CS0246:找不到类型或名称空间名称“ Quotes”。
HomeController.cs
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using DbConnection;
using Session.Models;
namespace Session.Controllers
{
public class HomeController : Controller
{
[HttpGet("quote")]
public IActionResult CreateQuote() {
return View();
}
[HttpPost("quote/Create")]
public void Create(Quotes quote) {
System.Console.WriteLine("Hi!!!!");
System.Console.WriteLine("name is: "+quote.name);
}
[HttpGet("test")]
public IActionResult test(Quotes obj) {
ViewBag.name = obj.name;
ViewBag.quote = obj.quote;
System.Console.WriteLine(obj.name);
System.Console.WriteLine(obj.quote);
return View("test");
}
}
}
Quotes.cs
using System;
using System.ComponentModel.DataAnnotations;
namespace Session.Models
{
public class Quotes
{
//[Required]
[Display(Name = "Your Name:")]
public string name {get;set;}
//[Required]
[Display(Name = "Quote:")]
public string quote{get;set;}
public DateTime createdAt {get;set;}
}
}
CreateQuote.cshtml
@model Quotes
<form asp-action="Create" asp-controller="Home" method="post">
<span asp-validation-for="name"></span>
<label asp-for="name"></label>
<input asp-for="name"><br>
<span asp-validation-for="quote"></span>
<label asp-for="quote"></label>
<input asp-for="quote"><br>
<input value="Add Quote" type="submit">
</form>
答案 0 :(得分:1)
选项1
在您的CreateQuote.cshtml视图中将模型引用为@model Session.Models.Quotes
。
选项2
@model Quotes
@using Session.Models
答案 1 :(得分:1)