【问题标题】:How to get the value from a kendo Editor in my model如何从我的模型中的剑道编辑器中获取价值
【发布时间】:2013-01-02 12:54:14
【问题描述】:

我正在尝试在我的 ASP.NET MVC 应用程序中使用 Kendo UI 编辑器控件。直到现在都没有成功,因为我无法将编辑器中的值返回到控制器中的模型。

我的模型很简单(在我的网站上编辑一个html页面):

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

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

我的观点包括这段代码:

@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>
}

我希望控制器中的 post 方法可以填充模型。如果我为名称和标题添加简单的编辑器(在示例代码中它们是隐藏的),它工作正常,但内容总是返回为空。

这是我的控制器方法:

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

//save content in a file

return View("CustomPages");
}

我错过了什么?我想我需要一些javascript来从编辑器中获取值,但我不知道如何实现它。

欢迎任何帮助。谢谢

【问题讨论】:

    标签: asp.net-mvc razor editor kendo-ui


    【解决方案1】:

    将您的编辑器命名为“内容”。真的。 :)

    编辑

    @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>
    }
    

    【讨论】:

    • 建设性反馈是可以的,但你不妨自己解释一下。这根本没有帮助。
    • 它成功了。现在看来我必须对返回的 HTML 进行编码。为什么会造成名称问题?
    • 这只是剑道的一个怪癖,名称是模型中的哪个字段被赋予了剑道工作深度的值。由于您使用的是 EditorFor,因此您甚至可能无法使用 .Name ,然后优先级应该落在指定的字段上——我认为。我总是明确地命名,但只是为了确定。
    • 删除 .Name() 对我有用。没有将编辑器命名为“内容”。
    • 我在最近的版本中没有遇到过这个问题,不确定是哪一个发生了变化——可能一年多了。现在,如果你使用 EditorFor,就像乔提到的那样,不要同时使用 .Name()。
    【解决方案2】:

    我遇到了同样的问题,在使用和 EditorFor 时我可以解决这个问题的唯一方法是根本不填充 Name 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-16
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-05
      相关资源
      最近更新 更多