简体   繁体   中英

Textarea won't show anything inside of it when used with asp-for in razor-pages ASP.NET Core MVC

The HTML tag <input> works fine and displays the data inside of it, but when I am using <textarea> it doesn't work and it probably does not work with asp-for but I'm still forced to use asp-for because I'm using .NET Core to build the web-page.

This is the output code that is malfunctioning (.cshtml)

@foreach (ComplaintManagement.Context.notesData note in ViewBag.getID)
                    {
                        <label>Subject</label>
                        <input required="required" class="form-control" asp-for="subject" value="@note.subject" />
                        <label>Summary</label>
                        <textarea class="form-control mb-2" asp-for="summary" rows="10">@note.summary</textarea>
                    }

If I failed to explain what I meant, hope the picture I attached helps... 在此处输入图片说明

If more information is needed, please ask in the comment section.

I found out an answer myself, instead of using asp-for I used this

<label>Summary</label>
<textarea class="form-control mb-2" rows="10" name="@Html.NameFor(m => m.summary)">@note.summary</textarea>

Using name="" instead of asp-for="" for the textarea solved the issue.

In normal scenario:

<textarea name='awesome'>Default value</textarea>

But in your case:

.NET Core developers who use asp-for must use a constructor in the model class to set the default value.

Example:

public class Service()
{
    public string Description { get; set; }
    //other properties

    public Service() //the constructor
    {
      Description = "Put the default value here!";
      //other default values
     }
}

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