【问题标题】:How to fill the text area by some data when MVC create form loading?MVC创建表单加载时如何用一些数据填充文本区域?
【发布时间】:2015-08-28 12:32:59
【问题描述】:

我有一个使用 MVC 创建的创建表单。该表单中有文本区域。我希望在加载表单时显示预填充的数据(非提示文本)。这是我视图中的文本区域。

  <div class="editor-field">
       @Html.EditorFor(model => model.message.Replace(Environment.NewLine, "<br/>")), new { rows = "6", cols = "500" })
       @Html.ValidationMessageFor(model => model.message)
  </div>

【问题讨论】:

  • 使用TextAreaFor,顺便问一下,现在发生了什么?有什么错误吗?
  • 那么你的问题是什么?你有什么问题?
  • 不,没有错误。工作完美。最初加载表单时,文本区域内没有记录。那是通常的情况对吗?所以我想在加载表单时已经用一些文本填充该文本区域,而不是让它为空。明白了吗??
  • 然后把你的属性值设置成你要显示的值
  • 在GET方法中,设置model.message = "some value";的值,然后在视图中使用@Html.TextAreaFor(m =&gt; m.message)

标签: html asp.net-mvc model-view-controller code-first


【解决方案1】:

如果要设置默认消息,请在控制器中进行

//GET
public ActionResult Create(string default_value)
{
  MyEntity newMyEntity = new MyEntity();
  newMyEntity.message= default_value;

  return View(newMyEntity);
}

或者如果它是静态文本,

 <div class="editor-field">
   @Html.EditorFor(model => model.message.Replace(Environment.NewLine, "<br/>")), new { rows = "6", cols = "500", @Value = "SomeText" })
   @Html.ValidationMessageFor(model => model.message)

【讨论】:

  • 为什么需要将默认值传递给控制器​​?
  • 静态方式不起作用。如果我使用第一个选项,我如何将消息设置到文本区域。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-11
  • 2017-07-04
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2020-11-08
相关资源
最近更新 更多