如何将 ViewBag 用于 Html.TextAreaFor()

How to using ViewBag for Html.TextAreaFor()

家庭控制器

public ActionResult Index()
        {
            ViewBag.LongText = Feedback.GetComment(Id); //calling a function from another class file
            return View(); 
        }

这是我用于 Html.TextArea 的代码,它只能显示 ViewBag 数据而不能 POST 数据

@Html.TextArea("txtComment", (string)(ViewBag.LongText), new { @class = "form-control"})

这是Html.TextAreaFor可以POST数据不能显示ViewBag数据

@Html.TextAreaFor(m => m.txtComment, new { @class = "form-control"})

我尝试添加 **@value="@ViewBag.txtContent" 但它不起作用。

在实际执行 POST 时,元素最重要的属性是名称属性。 name 属性是 MVC 用来构建将发送到表单的键和值字典的内容。

尝试向您的 TextArea 添加名称属性。

@Html.TextArea("txtComment", (string)(ViewBag.LongText), new { @class = "form-control", name = "txtComment"})