从控制器类中设置TexArea的值

本文关键字:TexArea 的值 设置 控制器 | 更新日期: 2023-09-27 18:12:15

我有更新的字符串在我的控制器类。我想把View中的TextArea设为这个字符串。我的控制器的ActionResult看起来像这样:

[HttpPost][STAThread]
    public ActionResult Index(DropDownModel model)
    {

        System.Diagnostics.Debug.WriteLine(DataJoin.Connector.data);

        return View();
    }

其中的data是我想在我的文本区域中设置的字符串。

我的视图是这样的:

@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<div> 
@Html.TextArea("textarea","Waiting for user input.....",new {style = "width: 1188px; height:280px;"})

如何设置这个TextArea中数据的值?

从控制器类中设置TexArea的值

只需将模型传递给post方法,如下所示

    [HttpPost]
    public ActionResult Index(DropDownModel model)
    {
        return View(model);
    }

然后在视图中输入

@model MvcApplication1.Models.DropDownModel
@Html.TextAreaFor(x => x.Text);
编辑:如果你谈论的是"System.Diagnostics.Debug.WriteLine(DataJoin.Connector.data);"而不是你的模型数据,那么你需要将该数据填充到viewModel中,并将该viewModel传递给视图,而不是"DropDownModel"。视图模型将包含下拉框和调试数据的属性。就像控制器

[HttpPost]
public ActionResult Index(ViewModel model)
{
    model.Data = DataJoin.Connector.data;
    return View(model);
}
<<p> 视图/strong>
@model MvcApplication1.Models.ViewModel
<h2>title</h2>
@Html.TextAreaFor(x => x.Data);

public class ViewModel
{
    public string Data { get; set; }
    public DropDownList DropDown { get; set; }
}

如果你正在寻找最简单的方法来做到这一点,使用ViewBag。

将字符串分配给一个ViewBag变量,类似于。

ViewBag.Diagnostics
在您的视图中,您可以通过 检索它。
@ViewBag.Diagnostics

正确的方法是将数据传递给视图模型,然后将其发送给视图。