This is a migrated thread and some comments may be shown as answers.

Problems when ModelState is not valid

3 Answers 682 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Huw Lloyd
Top achievements
Rank 1
Huw Lloyd asked on 22 Nov 2013, 04:51 PM
Hi

I have a simple MVC page for adding an article with a text box for the title and a Kendo editor for the content both bound to a model.

If I have validation rules on the title text box such as required field then when I post the form to the server it correctly shows an error message, however the text I wrote in the kendo ui editor is changed to show the html markup.

How do I ensure that what is displayed in the Kendo UI editor is as I wrote it (i.e. without the html markup) when the MVC Controller returns the model to the page if Model.IsValid == false

Many thanks in advance

3 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 26 Nov 2013, 01:58 PM
Hello,

Please check this documentation topic which provides information on why this issue occurs and how to avoid it.

Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Huw Lloyd
Top achievements
Rank 1
answered on 02 Jan 2014, 10:58 AM
Thanks for the help, however when I use the Encode(false) and AllowHtml option as suggested I get

A potentially dangerous Request.Form value was detected from the client


I don't like the sound of that :)

The alternative, to clear the model state is also no good because I lose the displaying of why the model failed validation in the first place

Is there some kind of example showing exactly how to implement these suggestions?

0
Daniel
Telerik team
answered on 06 Jan 2014, 08:31 AM
Hello,

I attached a sample project which demonstrates how to use the AllowHtmlAttribute. You can also use the ValidateInputAttribute on the action method in order to prevent the validation. 
As for clearing the state - besides clearing the entire state you can also remove just the key for the property that contains the HTML value so that the decoded value can be populated after postback:
ModelState.Remove("HtmlField");
model.HtmlField = HttpUtility.HtmlDecode(model.HtmlField);


Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Editor
Asked by
Huw Lloyd
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Huw Lloyd
Top achievements
Rank 1
Share this question
or