How to get the value from a kendo Editor in my mod

2019-07-04 04:06发布

问题:

I am trying to use a Kendo UI Editor control in my ASP.NET MVC application. No success until now, since I cannot manage to get the value in the editor back to the model in the controller.

My model is very simple (to edit an html page in my website):

public class EditedPage
{
public string Name { get; set; }
public string Title { get; set; }

[AllowHtml]
public string Content { get; set; }
}

And my view includes this code:

@model Page

<h2>@Model.Title</h2>
@using (Html.BeginForm())
{
    @Html.HiddenFor(m => m.Name)
    @Html.HiddenFor(m => m.Title)

    @(Html.Kendo().EditorFor(m => m.Content)
    .Name("Editor")
    .HtmlAttributes(new { style = "width:800px;height:600px;margin-left:20px;" })
    )

    <div>
        <input type="submit" value="@Resources.StringResources.Save" class="k-button"/>
    </div>
}

I was expecting the post method in the controller to get the model filled. If I add simple editors for Name and Title (in the sample code they are hidden) it works fine, but Content always comes back as null.

Here is my controller method:

[HttpPost]
public ActionResult EditPage(Page page)
{
if (!ModelState.IsValid)
 return View(page);

//save content in a file

return View("CustomPages");
}

What am I missing? I guess that I need some javascript to get the value from the editor, but I don't know how to achieve it.

Any help would be welcome. Thanks

回答1:

Name your editor 'Content'. Really. :)

EDIT

@model Page

<h2>@Model.Title</h2>
@using (Html.BeginForm())
{
    @Html.HiddenFor(m => m.Name)
    @Html.HiddenFor(m => m.Title)

    @(Html.Kendo().EditorFor(m => m.Content)
    .Name("Content")
    .HtmlAttributes(new { style = "width:800px;height:600px;margin-left:20px;" })
    )

    <div>
        <input type="submit" value="@Resources.StringResources.Save" class="k-button"/>
    </div>
}


回答2:

I had the same issue, and the only way I could get this resolved when using and EditorFor was to not populate the Name property at all.