【问题标题】:Kendo UI Grid passes null viewmodel to my ControllerKendo UI Grid 将空视图模型传递给我的控制器
【发布时间】:2014-02-12 02:44:07
【问题描述】:

这让我抓狂,我在超过 6 个项目中使用了 Kendo UI Grid,现在使用的是 2013.1.514.340 版本,我从来没有遇到过任何问题,但是今天早上我尝试在一个新项目中使用更新的版本 2013.2.918.340 并且我有一个大问题。

有了这个新项目,我有一个网格,没什么特别的。它的作用是在名为 LogTypes 的数据库中表示一个表,您可以在其中添加、编辑和删除项目。检索数据和删除方法有效,但创建方法给了我空值

这是截图

另一个有趣的事情是更新方法根本不调用 SaveOrUpdate。
客户端验证也有效。

这是我的完整代码,希望有人能帮助我:(

我的视图模型

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

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

}

我的控制器

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());
}

我的观点

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

我在做什么有问题吗?为什么它不触发更新,为什么创建传递空值?

更新

我也这样试过

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

还是不行

【问题讨论】:

    标签: c# asp.net-mvc kendo-ui kendo-grid


    【解决方案1】:

    解决了!我错过了添加对

    的引用

    kendo.web.min.js

    【讨论】:

    • 我遇到了一个问题,即编辑器值不会在我的视图模型中发布。但是,如果我的目录引用了模型本身,它就会通过。非常令人沮丧,因为我总是尝试在我的视图中使用视图模型。
    猜你喜欢
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多