如何将ViewBag用于Html.TextAreaFor

问题描述

HomeController

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

这是我用于 Html.TextArea 代码,该代码只能显示ViewBag数据,而不能发布数据

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

这是 Html.TextAreaFor ,它可以发布数据而不能显示ViewBag数据

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

我尝试添加** @ value =“ @ ViewBag.txtContent”,但无法正常工作。

解决方法

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

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

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