MVC从Controller类设置TextArea的值

时间:2014-07-08 21:15:22

标签: c# asp.net-mvc razor textarea

我的Controller类中有更新的字符串。我想将我的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中设置数据的值。

2 个答案:

答案 0 :(得分:0)

您只需将模型传递给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”。您的视图模型将包含dropdownn和调试数据的属性。像这样

<强>控制器

[HttpPost]
public ActionResult Index(ViewModel model)
{
    model.Data = DataJoin.Connector.data;
    return View(model);
}

查看

@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; }
}

答案 1 :(得分:-1)

如果您正在寻找最简单的方法,请使用ViewBag。

将字符串分配给ViewBag变量,例如。

ViewBag.Diagnostics

在您的视图中,您可以通过

检索它
@ViewBag.Diagnostics

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