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>
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 keyJobTask_CatToolIDit probably finds no matching property in the model and hence it is not assigned to any property. you can check this by omitting theName(---)method and then posting data to your controller.