2011-09-07 35 views
0

我在通過Ajax調用將我的模型從我的視圖中傳遞到控制器時出現問題。所有具有Telerik html'For'控件的模型屬性都不會保留在模型中。我可以在控制器中訪問這些值的唯一方法是使用Request [「control_name」]。所有其他標準控件,如輸入類型=文本序列化就好了。我究竟做錯了什麼?無法在Ajax調用中序列化Telerik MVC控件

這裏是我的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> 

回答

2

我相信.Name("JobTask_CatToolID")是問題的根源。當您將組合框的名稱屬性更改爲屬性名稱以外的名稱時,它不會自動通過modelbinder綁定到該屬性。 ModelBinder只查看發佈的鍵,然後在模型中搜索匹配的屬性並相應地填充它們。當聯編程序找到鍵JobTask_CatToolID時,它可能在模型中找不到匹配的屬性,因此它未分配給任何屬性。您可以通過省略Name(---)方法並將數據發佈到您的控制器來檢查。

相關問題