Skip to content

Textarea tagHelper is not rendering the contents of the element correctly. #4852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
colhountech opened this issue Sep 8, 2017 · 10 comments
Closed
Labels
affected-few This issue impacts only small number of customers area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-mvc-razor-views Features related to the Razor view engine for Razor pages and MVC views Needs: Design This issue requires design work before implementating. severity-minor This label is used by an internal tool

Comments

@colhountech
Copy link

The <textarea> tag helper is not rendering the content of the element correctly for a Razor Page.

If a Model property called Messages is modified in the OnPost, this update is visible in the view by using the razor directive @Model.Messages, but the <textarea> element does not display the correct innerHtml content.

This issue has been previously raised and closed because it could not be replicated.
aspnet/Mvc#3964

This example reproduces the issue:
https://github.com/colhountech/TextAreaDemo

Here is my attempt that fixes the scenario. I have not looked at regression so don't know yet if it
affects any other tests.

https://github.com/colhountech/Mvc/pull/1/files

basically it comes down to the following code segment in the

            ModelStateEntry entry;
            viewContext.ViewData.ModelState.TryGetValue(fullName, out entry);

            var value = string.Empty;
            if (entry != null && entry.AttemptedValue != null)
            {
                value = entry.AttemptedValue;
            }
            else if (modelExplorer.Model != null)
            {
                value = modelExplorer.Model.ToString();
            }

The issue seems to be related to both the ViewData and the modelExplorer returning a valid value, however the modelExplorer has the correct value, and this condition is never satisfied because the preceding condition is always true.

My fix for this issue is to remove the else, and allow the Model to take precedence over the ViewData ( I'm not sure if this effects regression), as follows:

             ModelStateEntry entry;
            viewContext.ViewData.ModelState.TryGetValue(fullName, out entry);

            var value = string.Empty;
            if (entry != null && entry.AttemptedValue != null)
            {
                value = entry.AttemptedValue;
            }
            // should the convention be that the Model override an ViewData for Razor Pages?
            if (modelExplorer.Model != null)
            {
                value = modelExplorer.Model.ToString();
            }

Hope this is an accurate understanding of the issue, if not please feel free to close.

@Eilon
Copy link
Contributor

Eilon commented Sep 11, 2017

If I understood this correctly, this is currently by design, though I confess I easily see why it's confusing.

ModelState (which is part of ViewData) always overrides model values because it represents the "in progress" value of the field. For example, if a user typed dog into an integer field, it is invalid (of course), but ModelState will cause the value dog to be re-rendered into the field's textbox, which allows the user on the site a chance to re-edit their data.

@Eilon
Copy link
Contributor

Eilon commented Sep 15, 2017

We think we might be able to build some APIs that make this easier, but they would have discoverability problems. For example, we could add some APIs that let you do things like:

ModelState.UpdateValue("SomeProperty", newValue);
or
ModelState.UpdateValue<MyModel>(m => m.SomeProperty, newValue);

What these end up doing is quite simple (though admittedly also difficult to discover), which is set ModelState["SomeProperty"].RawValue = newValue.

For the reasons mentioned above, moving this to Backlog.

@colhountech
Copy link
Author

Appreciate your response @Eilon and apologies for not responding sooner.

It's subtle, but I understand.

For now, I can just use the AttemptedValue to force an update to the contents of the TextArea as below, since the RawValue is never correct in my case.

Thanks for the clarification.

ModelState["Messages"].AttemptedValue  += $"OnPost: Updated on Server";

Since this is probably not a common scenario and the workround is fine, it should probably be closed.

@Eilon
Copy link
Contributor

Eilon commented Sep 25, 2017

@colhountech thanks for responding! We'll leave this open for at least a while to see if demand for this spikes - we often close Backlog items if they're largely untouched over at least a few months.

@colhountech
Copy link
Author

@Eilon, For reference, in case anyone else has the same issue, I've update the demo to show what I was trying to do, and what I needed to change in order to update the TextArea content from the OnPost() method of the Razor Page.

See here.

https://github.com/colhountech/TextAreaDemo/blob/master/README.md

@francescocristallo
Copy link

I have the same problem. Simple model on a cshtml page (without the @page at the top), the text is rendered into an input tag helper but not in the text area.

<input asp-for="@Model.Content" />
<textarea rows="3" asp-for="@Model.Content" />

On the same page the input works, the text area not.

@isaksky
Copy link

isaksky commented Jun 4, 2018

I'm getting the same issue as @francescocristallo .

The input behavior (where asp-for is "bidirectional") is what I (and I think most people) would expect. The textarea behavior seems really odd. Is there an advantage to this inconsistency, and making data normalization harder?

@aspnet-hello aspnet-hello transferred this issue from aspnet/Mvc Dec 13, 2018
@aspnet-hello aspnet-hello added this to the 3.0.0-preview2 milestone Dec 13, 2018
@aspnet-hello aspnet-hello added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates enhancement This issue represents an ask for new feature or an enhancement to an existing one Needs: Design This issue requires design work before implementating. PRI: 2 - Preferred labels Dec 13, 2018
@mkArtakMSFT mkArtakMSFT modified the milestones: 3.0.0-preview4, Backlog Feb 22, 2019
@Ponant
Copy link
Contributor

Ponant commented Mar 25, 2019

The following
<textarea asp-for="Input.Message" class="form-control" rows="5" cols="33">Hello there</textarea>
will not render the text "Hello there".
But the following will work
<textarea class="form-control" rows="5" cols="33">Hello there</textarea>
It is odd indeed it shows that asp-for is somewhat limiting the use of the textarea

@captainsafia captainsafia added affected-few This issue impacts only small number of customers severity-minor This label is used by an internal tool labels Nov 12, 2020
@javiercn javiercn added the feature-mvc-razor-views Features related to the Razor view engine for Razor pages and MVC views label Apr 19, 2021
@ysiivan
Copy link

ysiivan commented Aug 31, 2021

I don't often work with Razor, but there seem to be a rather misleading promise re: bound properties.

If a bound property is modified on a GET, then the new value are showing on screen.

However, if a bound property is modified in a POST handler, the new value does NOT make it to screen, regardless of whether the type of control is a textarea or an input box (or other).

The solution is to clear the model state, before returning from the post handler.
Doesn't feel like a complete "binding" solution.

@pranavkm pranavkm modified the milestones: Backlog, .NET 7 Planning Oct 19, 2021
@mkArtakMSFT
Copy link
Contributor

Hi. Thanks for contacting us.
We're closing this issue as there was not much community interest in this ask for quite a while now.
You can learn more about our triage process and how we handle issues by reading our Triage Process writeup.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affected-few This issue impacts only small number of customers area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-mvc-razor-views Features related to the Razor view engine for Razor pages and MVC views Needs: Design This issue requires design work before implementating. severity-minor This label is used by an internal tool
Projects
None yet
Development

No branches or pull requests