Home > Software design >  How do i prepopulate textarea with data from database (or any text for that matter) in ASP.Net?
How do i prepopulate textarea with data from database (or any text for that matter) in ASP.Net?

Time:05-11

Ok, so basically i have an form which edits data in my database. I have the following inputs:

   <input value="@title" type="text" asp-for="@Model.ProjName"  placeholder="Ticket1" />
   <textarea  value="@description" type="text" asp-for="@Model.ProjDescription"  rows="3"></textarea>

I can pre-populate the input just fine, however value doesn't work on the textarea. What is the correct way of populating it?

CodePudding user response:

This will solve your problem. Text area acts differently from input tag . So instead of using value parameter, add your value between open and close tag of text area

 <textarea  type="text" asp-for="@Model.ProjDescription"  rows="3">@Model.description</textarea>

Here is a .net fiddle with an example https://dotnetfiddle.net/2TYEOk

CodePudding user response:

Since you use asp-for="@Model.ProjDescription",so the default value will be @Model.ProjDescription,if you want to bind value with @description,you can try to use id and name to replace asp-for="@Model.ProjDescription".Here is the sample code:

<textarea type="text" id="ProjDescription" name="ProjDescription"  rows="3" >@description</textarea>
  • Related