Asp.net mvc 如何将文本从控制器分配到asp.net mvc视图中的文本区域(任何多行文本)?
我需要将数据从C中的控制器设置到asp.net mvc razor page.cshtml中的文本区域中。我的代码片段如下所示: 我无法将数据分配到模型中 在cshtml中:Asp.net mvc 如何将文本从控制器分配到asp.net mvc视图中的文本区域(任何多行文本)?,asp.net-mvc,Asp.net Mvc,我需要将数据从C中的控制器设置到asp.net mvc razor page.cshtml中的文本区域中。我的代码片段如下所示: 我无法将数据分配到模型中 在cshtml中: @Html.TextBoxFor(b => b.query, new { Value = ViewBag.query, height = "500px;" }) 在控制器中: ViewBag.query = "Hello"; model.query = ViewBag.query; 在模型中: [Display(
@Html.TextBoxFor(b => b.query, new { Value = ViewBag.query, height = "500px;" })
在控制器中:
ViewBag.query = "Hello";
model.query = ViewBag.query;
在模型中:
[Display(Name = "Query")]
public string query { get; set; }
您可以按以下方式定义多行文本框:
@Html.TextAreaFor(x => x.Body, 10, 15, null)
我看不出在控制器中为字段分配了值时,为什么要使用Value参数。我不确定这为什么不起作用,因为您没有提供完整的代码,所以我将尝试填补空白 型号:
public class ModelName
{
[Display(Name = "Query")]
public string query { get; set; }
}
在控制器中,确保返回模型
public class ControllerName : Controller
{
public ActionResult ActionName()
{
ViewBag.query = "Hello";
var model = new ModelName
{
query = ViewBag.query
}
return View(model)
}
}
在视图中,确保在页面上引用了对象类型
@model ApplicationName.Models.ModelName
因此,如果在视图中的任何位置,都可以通过这种方式获取文本区域的值
@Html.TextAreaFor(model => model.query, new { @class = "form-control", placeholder = "Add Placeholder", id = "textarea", autocomplete = "off", @rows = "10", style = "width: 70%; max-width: 100%;" }
根据需要,您可以随时更改样式或根本不使用任何样式 在html模型中=>model.query不工作。第一次只返回','然后如果我再次单击,然后返回',您好,'然后',您好,您好,'等等。在文本框中,因为它工作正常。实际上,我需要一个多行文本框控件在cshtml页面,其中必须显示文本Hello从controller@subhayan你能用问题的截图来编辑这个问题吗。另外,请您为代码添加更多上下文,以便我可以查看是否缺少某些内容。不确定为什么它适用于textbox而不适用于textarea