使用HTML填充ViewBag

时间:2011-12-20 14:25:25

标签: html asp.net-mvc viewbag

我有一个控制器,当点击我的表单上的按钮时,会调用该方法。 它在我的viewbag(一个包含html的字符串)中填充变量,然后我尝试在我的视图中显示它的内容。出于某种原因,viewbag的内容没有变化 这是我的控制器中的代码:

    Function Index() As ActionResult
        Dim TotalPSys As MyBusinessLayer.ListPSysAndMods = New ListPSysAndMods 
        ViewBag.HTMLForMods = "set"
        ViewBag.Test = "123"
        Return View(TotalPLC)
    End Function

  <HttpPost()>
    Function ShowModulesForPSys(ByVal strPSysID As String) As ActionResult
// .... do something....
        returnHTMLString = "<table><tr> <td>Show mods</td><td>Module Name</td></tr>"
        For Each moduleitem In modulelist
            returnHTMLString = returnHTMLString + " <tr> <td width='50%'  style='background-color:#5c87b2'><font color='white'>Number:</font> </td><td>Html.DisplayFor(Function(x) " + moduleitem.SlotNumber + ")</td>"
            returnHTMLString = returnHTMLString + "</tr><tr><td width='50%' style='background-color:#5c87b2'><font color='white'>RevNumber:</font></td>  <td>Html.DisplayFor(Function(x) " + moduleitem.RevisionNumber + ")</td>"
            returnHTMLString = returnHTMLString + "</tr><tr> <td width='50%'  style='background-color:#5c87b2'><font color='white'>IP Address:</font></td><td>Html.DisplayFor(Function(x) " + moduleitem.ModuleIP + ")</td></tr>"

        Next
        returnHTMLString = returnHTMLString + "</table>"
        'ViewData("HTMLForMods") = returnHTMLString
        ViewBag.HTMLForMods = returnHTMLString
        'MsgBox(ViewBag.HTMLForMods)
        ViewBag.Test = "456"
        MsgBox(ViewBag.Test)
        Return RedirectToAction("Index")

    End Function

视图中的代码如下所示:

         <p>@Html.Raw(ViewBag.HTMLForMods)</p>
         <p>@ViewBag.Test</p>

当系统在控制器代码中显示消息框时,它会显示正确的值。但是当视图显示时,它会显示viewbag数据的正确初始值,然后当我按下提交按钮时,控制器中的代码会正确执行,但是viewbag会显示旧数据。

1 个答案:

答案 0 :(得分:1)

您似乎正在重定向到索引,因此您的 Viewbag.HtmlForMod 将被索引操作方法中定义的内容覆盖。

相关问题