我可以在编辑器中使用viewbag.somedata吗?

时间:2014-10-13 05:53:25

标签: asp.net-mvc razor html-helper

海派我有一个问题。如何在编辑器中使用viewdata / bag来显示来自db的数据?

        @using (Html.BeginForm())
         {
            @Html.Editor(ViewBag.Task1,"Task1");
            @Html.Editor(ViewBag.Task2,"Task2");
            <input type="submit" value="Save">
         }
像这样。它显示我的错误

2 个答案:

答案 0 :(得分:1)

@Html.Editor的第一个参数是string,因此如果您要渲染值为ViewBag.Task1的文本框,请使用

@Html.Editor("Task1");

答案 1 :(得分:0)

i found the answer of my question, i can declare string variable as viewbag.somedata and i can use this variable in editor

here:


          @{string task1 = ViewBag.Task1;string task2 = ViewBag.Task2}


           @using (Html.BeginForm())
           {
            @Html.Editor(task1,"Task1");
            @Html.Editor(task2,"Task2");           
            <input type="submit" value="Save">
           }