【问题标题】:Can't serialize Telerik MVC controls in Ajax call无法在 Ajax 调用中序列化 Telerik MVC 控件
【发布时间】:2011-09-07 12:36:04
【问题描述】:

我在通过 Ajax 调用将模型从我的视图传递到我的控制器时遇到问题。具有 Telerik html 'For' 控件的所有模型属性都不会保留在模型中。我可以在控制器中访问这些值的唯一方法是使用 Request["control_name"]。所有其他标准控件,如 input type=text 序列化就好了。我究竟做错了什么?

这是我的 ajax 调用:

function ImportLogFile() {

    $.ajax({
        url: '/Job/ImportLogFile',
        type: 'POST',
        data: $("form").serialize(),
        success: function (data)
        {
            $('body').css('cursor', 'auto');
            alert("Word Counts imported.");
        },
        error: function (xhr, status, error) 
        {
            alert(status + ": " + strip(xhr.responseText).substring(0, 1000) + "...");
        }
    });
}

控制器:

[HttpPost]
public ActionResult ImportLogFile(tblJobTask model)
{
    ...
}

查看:

@model viaLanguage.Jams.Data.tblJobTask

<html>
<head></head>
<body>

@using (Html.BeginForm())
{

    <label class="editorLabel">CAT Tool Type:</label>
        @{ Html.Telerik().ComboBoxFor(model => model.CatToolID)
                .Name("JobTask_CatToolID")
                .BindTo(new SelectList((IEnumerable)ViewData["CatTools"], "CatToolID", "Description"))
                .HtmlAttributes(new { style = "width:220px;" });
         }

<input id="btnImport" type="button" onclick="ImportLogFile();"  />

}

</body>
</html>

【问题讨论】:

    标签: asp.net-mvc ajax telerik-mvc


    【解决方案1】:

    我相信.Name("JobTask_CatToolID") 是问题的根源。当您将组合框的 Name 属性更改为属性名称以外的其他内容时,modelbinder 不会自动绑定到该属性。 ModelBinder 只是查看发布的键,然后在模型中搜索匹配的属性并相应地填充它们。当 binder 找到键 JobTask_CatToolID 时,它可能在模型中找不到匹配的属性,因此它没有分配给任何属性。您可以通过省略 Name(---) 方法然后将数据发布到您的控制器来检查这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-11
      • 2019-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多