简体   繁体   中英

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. 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

I want to display plain text inside textarea that i'm loading from controller through model in view. but it's not showing inside text area

You can try following way, if you would like to load textarea from your ViewBag

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:

在此处输入图像描述

From Model:

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.

 <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 })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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