Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8204309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:51:32+00:00 2026-06-07T07:51:32+00:00

I’m using an HtmlString property on my model like this public HtmlString Html {

  • 0

I’m using an HtmlString property on my model like this

public HtmlString Html { get; set; }

then I have an EditorTemplate that renders and html editor but when I use TryUpdateModel() I get an InvalidOperationException because no type converter can convert between these types String and HtmlString.

Do I need to create a custom model binder or is there another way?

UPDATE:

I’m trying to use HtmlString on my model, mostly for making it obvious that it contains HTML.
So this is what my complete model looks like:

public class Model {
    public HtmlString MainBody { get; set; }
}

and this is how I render the form:

@using (Html.BeginForm("save","home")){
    @Html.EditorForModel()
    <input type="submit" name="submit" />
}

I have created my own editor template called Object.cshtml so that the field MainBody can be rendered as a textarea.

My controller has a Save method that looks like this:

public void Save([ModelBinder(typeof(FooModelBinder))]Model foo) {
    var postedValue = foo.MainBody;
}

As you can see I have been playing around with a custom model binder that looks like this:

public class FooModelBinder : DefaultModelBinder {
    protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder) {
        if (propertyDescriptor.PropertyType == typeof(HtmlString)) {
            return new HtmlString(controllerContext.HttpContext.Request.Form["MainBody.MainBody"]);
        }
        return null;
    }
}

this works as expected but I don’t know how to get the complete ModelName from the bindingContext because bindingContext.ModelName only contains the MainBody and not MainBody.MainBody?

I’m also interested in other solutions regarding this or maybe if someone thinks it’s a really bad idea.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:51:33+00:00Added an answer on June 7, 2026 at 7:51 am

    Do I need to create a custom model binder

    Yes, if you want to use an HtmlString property on your view model because this class has no parameterless constructor and the default model binder has no clue how to instantiate it.

    or is there another way?

    Yes, don’t use HtmlString property on the view model. There might also be other ways. Unfortunately since you have provided strictly 0 information about your context and what precisely you are trying to achieve that’s all we could help you with so far.


    UPDATE:

    Now that you have shown a wee-bit of your code here’s a sample.

    Model:

    public class Model
    {
        public HtmlString MainBody { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new Model());
        }
    
        [HttpPost]
        public ActionResult Index([ModelBinder(typeof(FooModelBinder))]Model foo)
        {
            var postedValue = foo.MainBody;
            return Content(postedValue.ToHtmlString(), "text/plain");
        }
    }
    

    Model binder:

    public class FooModelBinder : DefaultModelBinder
    {
        protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
        {
            if (propertyDescriptor.PropertyType == typeof(HtmlString))
            {
                return new HtmlString(bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue);
            }
            return null;
        }
    }
    

    View (~/Views/Home/Index.cshtml):

    @using (Html.BeginForm())
    {
        @Html.EditorForModel()
        <input type="submit" name="submit" />
    }
    

    Custom object editor template in order to do a deep dive (~/Views/Shared/EditorTemplates/Object.cshtml):

    @foreach (var property in ViewData.ModelMetadata.Properties.Where(x => x.ShowForEdit))
    {
        if (!string.IsNullOrEmpty(property.TemplateHint))
        {
            @Html.Editor(property.PropertyName, property.TemplateHint)
        }
        else
        {
            @Html.Editor(property.PropertyName)
        }
    }
    

    Custom editor template for the HtmlString type to be rendered as a textarea (~/Views/Shared/EditorTemplates/HtmlString.cshtml):

    @Html.TextArea("")
    

    By the way I still don’t understand why you would want to use HtmlString as a property instead of a simple string but anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.