Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How use Kendo UI Editor in asp.net mvc3 with razor?

I'm using editor from Kendo UI, so I have big problem.

I don't know how display items which are returned by editor.

Editor convert something like:

<img src="someurl" />

to:

lt;p&gt;&lt;img src=&quot;someurl&quot;/&gt;&lt;/p&gt;

and I keep converted string in database, and try display it with:

@Html.Raw(item.description)

where description is string returned by kendo.

So I have no idea how display it correctly in my View

Any help would be appreciated.

like image 966
Mateusz Rogulski Avatar asked Jun 21 '12 06:06

Mateusz Rogulski


4 Answers

There is an option of the KendeUI editor called encoded which configures whether the Editor should submit encoded HTML tags or not.

The default value for encoded is true

If you wan't to store the unencoded text use this sniplet when creating your editor:

$("#Editor").kendoEditor({
     encoded: false
 });

But because you are not sending encoded text to the server the Asp.net request validator kicks in and it will abort your request.

If you are using strongly typed views what you can do is to use the AllowHtmlAttribute on your model property:

View:

@model MyModel

@using(Html.BeginForm("SomeAction", "SomeController"))
{
     @Html.TextAreaFor(m => m.Editor)
     <input type="submit" value="Save" />
}

<script type="text/javascript">
   $(function(){
      $("#Editor").kendoEditor({
        encoded: false
      });
   });
</script>

Model:

public class MyModel
{
    [AllowHtml]
    public string Editor { get; set; }
}

Controller action

public ActionResult SomeAction(MyModel myModel)
{
    //Save to db, etc.
}

You also need to set the following in your web.config or this attribute won't have effect in .NET 4.0:

<httpRuntime requestValidationMode="2.0"/>
like image 106
nemesv Avatar answered Sep 20 '22 22:09

nemesv


I found this solution for MVC: in View

<div class="editor-field">
    @(Html.Kendo().EditorFor(model => model.HtmlField).Encode(false))
    @Html.ValidationMessageFor(model => model.HtmlField)
</div>

in model:

   [DataType(DataType.Html)]
   [AllowHtml]
   public string HtmlField{ get; set; }

That was enough

like image 22
Andrew Veriga Avatar answered Sep 17 '22 22:09

Andrew Veriga


Simplier way to do it is make changes in controller, no in view and model. So:

View

$("#Editor").kendoEditor();

Model

public class MyModel
{
    public string Editor { get; set; }
}

Controller

Editor = Server.HtmlDecode(Editor);

HtmlDecode

like image 27
Mateusz Rogulski Avatar answered Sep 18 '22 22:09

Mateusz Rogulski


The editor templates generated from the .NET Wrappers aren't working any more. Here is a fix.

http://pknopf.com/blog/kendo-ui-editor-templates-for-asp-net

like image 43
Paul Knopf Avatar answered Sep 20 '22 22:09

Paul Knopf