c# - 表中的数据未出现在 ASP.NET 页面上
问题描述
很简单,当我的 ASP.NET MVC 页面被编译成 HTML 时,我在服务器上的表中有数据。
我知道后端的一切都有效,因为在其他视图上没有问题,但由于某种原因,在这一页上它不起作用。
我怀疑这与我的模型中有两个类有关,因为这是唯一的页面,但我不太确定,因为我是 MVC 的新手。
这是模型,我将其称为ModelFile.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Project.Subgroup.Models
{
public class ClassIActuallyNeed
{
public int Id { get; set; }
public string Html { get; set; } //<---This is the data I want
public string Version { get; set; }
public bool Deleted { get; set; }
}
public class ClassMyFriendInsistsINeed
{
public int Id { get; set; }
public string Name { get; set; }
public byte[] Signature { get; set; }
public bool Deleted { get; set; }
public virtual ClassIActuallyNeed ClassIActuallyNeed { get; set; }
}
}
...以及名为MainController.cs的控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project.Models;
using Project.Subgroup.Models;
using System.Data.Entity;
using Project.Models.Authentication;
namespace Project.Subgroup.Controllers
{
public class MainController : Controller
{
private MyDatabase context = new MyDatabase();
// GET: Lobby/Home
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult FormPage()
{
return View();
}
[HttpPost]
public ActionResult FormPage(ClassIActuallyNeed model)
{
if (ModelState.IsValid)
{
using (var context = new MyDatabase())
{
var modelfile = new ModelFile
{
Name = model.Name,
Signature = model.Signature,
ClassIActuallyNeedId = model.ClassIActuallyNeedId
};
}
}
return View(model);
}
}
}
...最后是视图,即FormPage.cshtml:
@model Project.Models.ClassIActuallyNeed
@{
ViewBag.Title = "My Page";
Layout = "~/Subgroup/Views/Shared/_Layout.cshtml";
}
<h2 >@ViewBag.Title</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.DisplayFor(model => model.Html) //<---- the problem
//There is other form stuff in here
}
“Html”数据实际上只是一个包含我想要放入我的网页的 HTML 的字符串。这是该表中唯一的一行,当我使用“DisplayNameFor”而不是“DisplayFor”时,它成功地将名称“Html”放在我的页面上。
对不起,代码呕吐,我删掉了我所知道的所有无关的东西,但我无法真正确定我的问题出在哪里。Visual Studio 或控制台中没有任何类型的错误。页面上的所有其他内容都运行良好,包括我的 JS 内容。
编辑:到目前为止,我感谢所有输入。我知道问题不在于我没有填充模型,因为它是由远程数据库填充的。这适用于所有其他页面。正如我怀疑 RequestVerificationToken 肯定应该在那里,并且不相关。
还要澄清一下:问题不在于它显示的是未格式化的文本而不是 HTML。问题是它根本没有显示任何内容。
解决方案
您目前有 2 个问题,我可以看到:
你没有用任何数据填充你的模型
[HttpGet] public ActionResult FormPage() { //you need to pass the view some data, otherwise your model in your view //will be null and you wont get any output (which is what you're seeing now) ClassIActuallyNeed model = null; //hardcoded: //model = new ClassIActuallyNeed //{ // Id = 1, // Html = "<h1>Hello</h1>", // Version = "something, // Deleted = false //}; //an example of using your context to fetch your model using(var context = new MyDatabase()) { var id = 1 model = context.ClassIActuallyNeeds.Single(x => x.Id == id); } return View(model); }
您没有将 html 字符串呈现为 html
Html.DisplayFor(model => model.Html)
将在页面上将此属性呈现为纯文本,因此您不会Hello
在标签中看到格式化,而是在页面上看到纯文本。该框架这样做是为了防止注入攻击,我建议您阅读一下,但是就本文而言,这超出了范围。要实际呈现 html 字符串,请使用<h1>
<h1>Hello</h1>
Html.Raw(model => model.Html)
作为附加说明,
<input name="__RequestVerificationToken" type="hidden" value="[a bunch of junk characters here]">
您看到的是您使用@Html.AntiForgeryToken()
. 防伪令牌用于防止 CSRF 攻击,我还建议您阅读(安全性很重要!)。由于此视图中没有任何可编辑的数据(这是我根据您使用 做出的假设DisplayFor
),因此我没有理由看到Html.BeginForm
您的视图中有表单 ( ) 或 AntiForgeryToken。如果此数据是可编辑的并且可以发布回服务器,则忽略这一点,这两个部分可能都需要保留在原位。
使用上面的控制器操作,您的视图可以简单地如下所示来显示您的数据:
@model Project.Models.ClassIActuallyNeed
@{
ViewBag.Title = "My Page";
Layout = "~/Subgroup/Views/Shared/_Layout.cshtml";
}
<h2 >@ViewBag.Title</h2>
@Html.Raw(model => model.Html)
如果这些数据需要可编辑,那么它看起来更像
@model Project.Models.ClassIActuallyNeed
@{
ViewBag.Title = "My Page";
Layout = "~/Subgroup/Views/Shared/_Layout.cshtml";
}
<h2 >@ViewBag.Title</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.Version)
@Html.HiddenFor(model => model.Deleted)
@Html.EditorFor(model => model.Html)
<input type="submit" value="Submit">
}
推荐阅读
- vb.net - 使用 VB.NET 在 MongoDB 中批量写入动态文档
- vue.js - 无法将数组从一个 Vue 组件推送到另一个。我哪里错了?
- docker-compose - 如何解决错误调用索引:无类型 nil 的索引?
- react-native - 聊天应用、React Native 和 FlaskBackend 以及 flask_socketio 的问题
- cordova - 在 www 中访问 phonegap 中的媒体项目
- c# - 如何快速选择数字和权重以获得特定的加权和?
- swift - 删除 NSCollectionView 中的部分和标题(粘性标题流布局)
- php - API 平台 - 序列化组无法正常工作
- flutter - 如何单独链接 Flutter StaggeredGridView 项目?
- python - python中的正则化超几何函数3F2