user2856028
user2856028

Reputation: 43

Posted model properties from kendo grid is null in controller action

I find that the posted model from kendo grid is null in the controller action. There are a lot of related forums here but nothing I could use as a solution to my issue.

This code in cshtml is as below:

@(Html.Kendo().Grid(Model.Skills)
.Name("skillGrid")
.Columns(columns =>
{
columns.Bound(p => p.SkillNameNew).ClientTemplate("#= SkillNameNew #" +
"<input type='hidden' name='Skills[#= index(data)#].SkillNameNew' value='#= SkillNameNew#' />")
.Title("Skill Name");

columns.Bound(p => p.SkillName).Hidden().ClientTemplate("#= SkillName #" +
"<input type='hidden' name='Skills[#= index(data)#].SkillName' value='#= SkillName #' />");

columns.Template(p => p.Proficiency).ClientTemplate("<div id='star' data-text='#: SkillName #' data-score='#: Proficiency #' role='rating' ></div>" +"<input type='hidden' id='prof' name='Skills[#= index(data)#].Proficiency' value='#= Proficiency #' />").Title("Proficiency");

columns.Bound(p => p.YearsOfExp).ClientTemplate("#= (YearsOfExp == null || YearsOfExp == ' ' || YearsOfExp == '') ? '' : kendo.parseInt(YearsOfExp) #" + "<input type='hidden' name='Skills[#= index(data)#].YearsOfExp' value='#= YearsOfExp #' />").Title("Years Of Exp");

columns.Bound(p => p.LastUsedYear).ClientTemplate("#= (LastUsedYear == null || LastUsedYear == '0' || LastUsedYear == '') ? '' : kendo.parseInt(LastUsedYear) #" +"<input type='hidden' name='Skills[#= index(data)#].LastUsedYear' value='#=LastUsedYear#' />").Title("Last Used Year");

columns.Command(command => command.Destroy()).Width(40);})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Events(events => events.DataBound("DataBound"))
.Resizable(re => re.Columns(true))
.Reorderable(reorder => reorder.Columns(false))
.Sortable()
.Scrollable(scroll => scroll.Virtual(true))
.Groupable()
.Filterable()
.HtmlAttributes(new { style = "height:100%;" })
.DataSource(d => d.Ajax()
                   .Batch(true)
                   .ServerOperation(false)
                   .PageSize(Constants.DEFAULT_PAGE_SIZE)
                   .Model(model => model.Id(l => l.SkillName))

The controller action has the below signature:

[HttpPost]
public ActionResult UpdateExperience(EditCandidateExperienceViewModel model)
{
------
}

Skills is a property of type "List" in "EditCandidateExperienceViewModel"

It looks like the model property names and the names bound in the grid are consistent. Please let me know what I could be missing here. Any help is greatly appreciated as I have run out of ideas on this.

Upvotes: 1

Views: 1353

Answers (2)

Anand Thakkar
Anand Thakkar

Reputation: 302

You have to pass list of model from the grid. for that you have to pass IEnumerable or IList type of list to the controller.

Upvotes: 1

Bishnu Rawal
Bishnu Rawal

Reputation: 1387

If Skills is List<T>, then decorate your action like:

[HttpPost]
public ActionResult UpdateExperience(T model)
{
  ------
}

You are giving grid the different model and forcing to post the parent model EditCandidateExperienceViewModel.

Upvotes: 1

Related Questions