【问题标题】:How use ViewBag to display data如何使用 ViewBag 显示数据
【发布时间】:2020-10-30 08:21:15
【问题描述】:

学校管理员

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,但还是不行!

【问题讨论】:

    标签: asp.net asp.net-mvc model-view-controller


    【解决方案1】:

    要在 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>
    

    【讨论】:

    • 对我不起作用它仍然没有返回任何我在 cshtml 和 html 上尝试过的东西
    • 你检查了吗,你在 dbschool.text_content 中获取数据?
    • dbschool.text_content 根本没有获取数据有什么原因吗?
    • 你先在 dbschool 中获取数据吗?
    猜你喜欢
    • 1970-01-01
    • 2018-01-08
    • 2012-05-18
    • 2021-10-16
    • 2016-06-07
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多