【问题标题】:MVC Setting the value of TexArea from Controller ClassMVC 从控制器类中设置 TextArea 的值
【发布时间】:2014-07-09 20:53:15
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    您只需像这样将模型传递给您的 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);
    }
    

    查看

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

    【讨论】:

      【解决方案2】:

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

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

      ViewBag.Diagnostics
      

      在您看来,然后您可以通过

      检索它
      @ViewBag.Diagnostics
      

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

      【讨论】:

      • 我认为最好从一开始就提出正确的方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 1970-01-01
      相关资源
      最近更新 更多