gpt4 book ai didi

c# - 在 View 中找不到 ASP.Net 核心模型

转载 作者:行者123 更新时间:2023-11-30 12:20:04 24 4
gpt4 key购买 nike

我正在复习一些 ASP.Net 核心教程。我正在和模型打交道。我创建了一个 Quote 模型来将名称和一些文本存储到数据库,但出现错误:

obj/Debug/netcoreapp2.1/Razor/Views/Home/CreateQuote.g.cshtml.cs(23,92): error CS0246: The type or namespace name 'Quotes' could not be found.

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");
}
}
}

报价.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;}
}
}

创建报价.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>

最佳答案

选项 3 - ViewImports

_ViewImports.cshtml

@using Session.Models

关于c# - 在 View 中找不到 ASP.Net 核心模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53508206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com