如何使用ViewBag显示数据

问题描述

SchoolController

schoolBLL content = new schoolBLL ();
DatabaseSchool  dbSchool = content.GetSchoolInfoById(LoginStaffId);

if (dbschool != null)
{ 
    string textcontent = dbschool .text_content;
}

课程文件

public DatabaseSchool GetSchoolInfoById(int StaffId)
{
    return SchoolRepo.Find(a => a.schoolcontent_id == StaffId).FirstOrDefault();
}

因此,我正在尝试使用此标记将数据显示到HTML页面

如何将值从控制器传递到HTML页面

我已经尝试使用 ViewBag ,它不起作用!

解决方法

要在html页面中显示数据,请尝试以下操作:

学校控制器:

schoolBLL content = new schoolBL();

DatabaseSchool dbSchool = content.GetSchoolInfoById(LoginStaffId);
    
if(dbschool != null)
{ 
   TempData["textcontent"] = dbschool.text_content;
}

HTML:

   <textarea id="txtcontent" class="form-control" rows="20">@TempData["textcontent"]</textarea>

更好地使用CSHTML:

@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    @Html.TextArea("Content",TempData["textcontent"])
</body>
</html>