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

删除回忆录丶 提交于 2019-12-10 20:15:30

问题


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.



来源:https://stackoverflow.com/questions/14405264/how-to-get-the-value-from-a-kendo-editor-in-my-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!