Raymund
Raymund

Reputation: 7892

Kendo UI Grid passes null viewmodel to my Controller

This is making me crazy, I used Kendo UI Grid in more than 6 projects now using the version 2013.1.514.340, I never had any issues but this morning I tried to use a newer version 2013.2.918.340 on a new project and I have a major issue.

With this new project I have a grid, nothing fancy. What it does is represent a table in a database called LogTypes where you can add, edit and delete items. Retrieving the Data and the Delete Method works but the Create Method gives me null values

Here is a screenshot enter image description here

Another funny thing is that the Update Method does not call the SaveOrUpdate at all.
Client side validation also works.

enter image description here

Here is my full code, I hope someone here can help me :(

My View Models

public class LogTypesPageViewModel
{
    public IList<LogTypesViewModel> LogTypes { get; set; }
}
public class LogTypesViewModel
{
    public int? LogTypeId { get; set; }

    [Required]
    public string LogTypeDescription { get; set; }

}

My Controller

public class LogTypesController : Controller
{
private readonly ILogTypesQuery logTypesQuery;
private readonly ICommandProcessor commandProcessor;

public LogTypesController(
    ILogTypesQuery logTypesQuery,
    ICommandProcessor commandProcessor)
{
    this.logTypesQuery = logTypesQuery;
    this.commandProcessor = commandProcessor;
}

public ActionResult Index()
{           
    var viewModel = new LogTypesPageViewModel
    {
        LogTypes = logTypesQuery.GetLogTypes()
    };

    return View(viewModel);
}

public ActionResult GetLogTypes([DataSourceRequest]DataSourceRequest request)
{
    var logTypes = logTypesQuery.GetLogTypes();
    var result = logTypes.ToDataSourceResult(request);

    return Json(result);
}

[HttpPost]
[Transaction]
public ActionResult SaveOrUpdate(LogTypesViewModel viewModel, [DataSourceRequest]DataSourceRequest request)

{
    if (ModelState.IsValid)
    {
        var command = new SaveOrUpdateLogTypeCommand(
                        viewModel.LogTypeId,
                        viewModel.LogTypeDescription
                        );

        if (ModelState.IsValid)
        {
            commandProcessor.Process(command);
            viewModel.LogTypeId = command.LogTypeId;
        }
    }

    var result = new[] { viewModel }.ToDataSourceResult(request, ModelState);

    return Json(result);
}       

[HttpPost]
[Transaction]
public ActionResult Delete(int logTypeId, [DataSourceRequest]DataSourceRequest request)
{
    var command = new DeleteLogTypeCommand(logTypeId);
    commandProcessor.Process(command);

    return Json(ModelState.ToDataSourceResult());
}

My View

@model ViewModels.LogTypes.LogTypesPageViewModel
@{
    ViewBag.Title = "Log Types";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>Log Types</h1>

@(Html.Kendo().Grid(Model.LogTypes)
    .Name("grid-log-types")
    .Columns(c =>
    {
        c.Command(commands =>
        {
            commands.Edit();
            commands.Destroy();
        }).Width(150);

        c.Bound(x => x.LogTypeDescription);
        c.Bound(x => x.LogTypeId);
    }) 
    .ToolBar(commands => commands.Create()) 
    .Editable(e => e.Mode(GridEditMode.InLine))   
    .Sortable()
    .Scrollable()
    .DataSource(d => d
        .Ajax()
        .Create(create => create.Action("SaveOrUpdate", "LogTypes"))
        .Update(update => update.Action("SaveOrUpdate", "LogTypes"))
        .Destroy(destroy => destroy.Action("Delete", "LogTypes"))
        .Read(read => read.Action("GetLogTypes", "LogTypes"))
        .Model(x => x.Id(p => p.LogTypeId))
        )
)

Is there something wrong in what I am doing? Why doesn't it trigger the Update and why Create passes null value?

UPDATE

I also tried it like this

@(Html.Kendo().Grid<ExternalUserManagement.Web.Mvc.Controllers.ViewModels.LogTypes.LogTypesViewModel>()

Still does not work

Upvotes: 1

Views: 4215

Answers (1)

Raymund
Raymund

Reputation: 7892

Solved it! I missed the adding the reference to

kendo.web.min.js

Upvotes: 0

Related Questions