无法在 Ajax 调用中序列化 Telerik MVC 控件

发布于 2024-12-03 16:04:13 字数 1378 浏览 5 评论 0原文

我在通过 Ajax 调用将模型从视图传递到控制器时遇到问题。所有具有 Telerik html“For”控件的模型属性不会保留在模型中。我可以访问控制器中这些值的唯一方法是使用 Request["control_name"]。所有其他标准控件(例如输入类型=文本)序列化都很好。我做错了什么?

这是我的 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>

I am having problems passing my model from my view via an Ajax call to my controller. All of the model properties that have Telerik html 'For' controls do not persist in the model. The only way I can access those values in the controller is using Request["control_name"]. All other standard controls like input type=text serialize just fine. What am I doing wrong?

Here is my ajax call:

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) + "...");
        }
    });
}

Controller:

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

View:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-12-10 16:04:13

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

i believe .Name("JobTask_CatToolID") is the source of problem. when you change Name attribute of combobox to something other than property name it will not automatically bound to the property by modelbinder. ModelBinder just looks at posted keys and then searches for matching properties in model and populates them accordingly. and when binder finds the key JobTask_CatToolID it probably finds no matching property in the model and hence it is not assigned to any property. you can check this by omitting the Name(---) method and then posting data to your controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文