1

I'm trying to generate a TextArea box with default value supplied by the controller. Given this markup which calls a partial view:

@{
    Html.RenderAction(
        "ContentBlock", "ContentBlock", 
            new TestSiteMvc.Models.ContentModel() { ContentID = 3 }
        );
}

Partial View:

@Html.TextBoxFor(m=>m.Title)
@Model.ContentHtml<br />
@Html.TextAreaFor(m => m.ContentHtml)
@Html.TextBoxFor(m => m.ContentHtml)
@Html.TextArea("ContentHtml2", Model.ContentHtml)
@Model.ContentHtml<br />

The resulting HTML rendered is:

<input id="Title" name="Title" type="text" value="Home Title" />
Home Page Content<br />
<textarea cols="20" id="ContentHtml" name="ContentHtml" rows="2"></textarea>
<input id="ContentHtml" name="ContentHtml" type="text" value="Home Page Content" />
<textarea cols="20" id="ContentHtml2" name="ContentHtml2" rows="2">Home Page Content</textarea>
Home Page Content<br />

So, it's clear the ContentHtml property is getting populated. What I don't understand is why TextAreaFor is getting rendered with an empty value. And to be clear, when I remove the line @Html.TextBoxFor(m => m.ContentHtml) as to not incur tags with the same id, the issue does not go away.

Controller:

    [HttpGet]
    public ActionResult ContentBlock(Models.ContentModel mcontent) {

        mcontent = new Models.ContentModel() { 
            ContentID = 3,
            ContentHtml = "Home Page Content", 
            Title = "Home Title" };
        return PartialView(mcontent);
    }

Model:

public class ContentModel {

    public int ContentID { get; set; }
    public string Link { get; set; }
    public string Title { get; set; }
    public string ContentHtml { get; set; }
    public DateTime LastModifiedDate { get; set; }
}
3
  • Cannot duplicate this at all. Commented Dec 19, 2016 at 20:48
  • Where is your controller code? Tested the view code using given viewmodel properties in question and it has worked, you need to provide controller action method to clear out textarea default value issue. Commented Dec 20, 2016 at 1:09
  • @TetsuyaYamamoto, I added the controller and some additional code. Thanks. Commented Dec 20, 2016 at 13:45

1 Answer 1

1

Your GET method has a parameter which is your model, so the DefaultModelBinder initializes ContentModel with default values and sets the value of ContentID to 3. At this point all values are added to ModelState (the value of ContentHtml in ModelState is null - the default value for string). Updating the values of your properties does not affect the values in ModelState.

When you return the view, the @Html.TextAreaFor() first checks ModelState, which in your case is null so no value is rendered. (and if a value in ModelState is not found, the method then checks ViewData, and finally the actual value of your model property to determine what to render).

Note also that @Html.TextArea("ContentHtml2", Model.ContentHtml) works because it is not binding to a model property and your using an overload that specifically sets the value based on another property.

The correct approach, and easiest way to solve this is to change the controller method to

public ActionResult ContentBlock(int id)
{
    ContentModel model = new ContentModel()
    { 
        ContentID = id,
        ContentHtml = "Home Page Content", 
        Title = "Home Title"
    };
    return PartialView(model);
}

and change the view code to

@{ Html.RenderAction( "ContentBlock", "ContentBlock", new { id = 3 }); }

Alternatively, you could add ModelState.Clear() as the first line of code in the controller method to remove all values from ModelState (the TextAeaFor() method will then get the value from the model property)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Changing the controller solved the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.