简体   繁体   English

html textarea 不显示 mvc 中 textarea 内的值

[英]html textarea not showing value inside textarea in mvc

I want to display plain text inside textarea that i'm loading from controller through model in view.我想在视图中显示从 controller 到 model 加载的纯文本。 but it's not showing inside text area但它没有显示在文本区域内

 @foreach (DsplayModel display in ViewBag.Controls) { @using (Html.BeginForm("ControllerAction", "ControllerToPost", FormMethod.Post} { //try 1 <textarea id="txtArea" class="form-control mt-15" rows="3" maxlength="500" asp-for="UserComment"> @display.UserComment </textarea> } //try 2 @Html.TextAreaFor(m => m.UserComment, new { id = "NameBox", placeholder = "Name", Value = @display.UserComment }) }

My 1st try render as a我的第一次尝试渲染为

<textarea id="txtArea" class="form-control mt-15" rows="3" maxlength="500" asp-for="UserComment"></textarea>

My 2nd try render as a我的第二次尝试渲染为

<textarea id="NameBox" name="UserComment" placeholder="Name" Value="Hello">  

// In 2nd try my value is loading in textarea ie Value="Hello" but not inside textbox // 在第二次尝试中,我的值加载到文本区域中,即 Value="Hello" 但不在文本框中

I want to display plain text inside textarea that i'm loading from controller through model in view.我想在视图中显示从 controller 到 model 加载的纯文本。 but it's not showing inside text area但它没有显示在文本区域内

You can try following way, if you would like to load textarea from your ViewBag如果您想从ViewBag加载textarea ,您可以尝试以下方式

Controller: Controller:

public IActionResult Index()
        {
            var dsplayModelList = new List<DsplayModel>()
            {
                new DsplayModel() { UserComment="This is some comment from viewbag"},
                new DsplayModel() { UserComment="This is another comment from viewbag"},

            };
            ViewBag.Controls = dsplayModelList;
            return View();
        }

View:看法:

@model DotNetWebApp.Models.DsplayModel

@foreach (DsplayModel display in ViewBag.Controls)
{

    @using (Html.BeginForm("ControllerAction", "ControllerToPost", FormMethod.Post))
    {
        <textarea name="NameBox" placeholder = "Name" class="form-control">@display.UserComment</textarea>
       
    }

}

Output: Output:

在此处输入图像描述

From Model:来自 Model:

Controller: Controller:

public IActionResult Index()
    {
        var dsplayModel = new DsplayModel();
        dsplayModel.UserComment = "This is some comment from Model";
        
        return View(dsplayModel);
    }

View:看法:

@model DotNetWebApp.Models.DsplayModel

@using (Html.BeginForm("ControllerAction", "ControllerToPost", FormMethod.Post))
{
    @Html.TextAreaFor(model => model.UserComment)

}

Note: If you need list, just use foreach loop.注意:如果你需要列表,只需使用 foreach 循环。

 <textarea class="form-control" rows="4">@Model.UserComment</textarea>
//Try 1
@foreach (DsplayModel display in @ViewBag.Controls)                                                         {
    <textarea id="txtArea" class="form-control mt-15" rows="3" 
    maxlength="500" 
   asp-for="UserComment">
    @display.UserComment
    </textarea>
}

//Try 2
@foreach (DsplayModel display in @ViewBag.Controls)                                                         {
   @Html.TextAreaFor(m => m.UserComment, new { id = "NameBox", placeholder = "Name", Value = @display.UserComment })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM