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.
Yes, if you want to use an
HtmlStringproperty on your view model because this class has no parameterless constructor and the default model binder has no clue how to instantiate it.Yes, don’t use
HtmlStringproperty 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:
Controller:
Model binder:
View (
~/Views/Home/Index.cshtml):Custom object editor template in order to do a deep dive (
~/Views/Shared/EditorTemplates/Object.cshtml):Custom editor template for the
HtmlStringtype to be rendered as a textarea (~/Views/Shared/EditorTemplates/HtmlString.cshtml):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.