如何在回调期间将模型绑定发送到devexpress gridview回到控制器?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在回调期间将模型绑定发送到devexpress gridview回到控制器?相关的知识,希望对你有一定的参考价值。

我想在回调期间将模型绑定传递给devexpress gridview,但是失败了。

楷模

public class UploadExcelError
{
    public int rowNumber { get; set; }
    public string errorMessage { get; set; }
}

public class UploadViewModel
{
    public List<UploadExcelError> UploadExcelErrors { get; set; }
    public UploadFile UploadFile { get; set; }
}

UploadErrorPartial.cshtml

@using ControlTower2.Models
@model ControlTower2.Models.UploadViewModel

@if (Model != null)
{
    if (Model.UploadExcelErrors != null)
    {

        @Html.DevExpress().GridView(
            settings =>
            {
                settings.Name = "gridViewExcelError";
                settings.CallbackRouteValues = new { Controller = "PurchaseOrder", Action = "FilteringError", xyz = Model };
                settings.SettingsBehavior.AllowSort = false;
                settings.SettingsPager.Mode = GridViewPagerMode.ShowAllRecords;
                settings.Columns.Add(column => { column.FieldName = "rowNumber"; column.Caption = "Excel Row#"; });
                settings.Columns.Add(column => { column.FieldName = "errorMessage"; column.Caption = "Error Message"; });
                settings.Settings.ShowHeaderFilterButton = true;
                var headerFilterMode = GridHeaderFilterMode.List;
                foreach (GridViewDataColumn column in settings.Columns)
                    column.SettingsHeaderFilter.Mode = headerFilterMode;
            }
        ).Bind(Model.UploadExcelErrors).GetHtml()
    }
}

调节器

public ActionResult FilteringError(UploadViewModel xyz)
{
    return PartialView("UploadErrorPartial", xyz);
}

xyz在上面的代码中为空。

我也尝试过:

settings.CallbackRouteValues = new { Controller = "PurchaseOrder", Action = "FilteringError", xyz = Model.UploadExcelErrors };

public ActionResult FilteringError(List<UploadExcelError> xyz)
{
    UploadViewModel uploadViewModel = new UploadViewModel();
    uploadViewModel.UploadExcelErrors = xyz;
    return PartialView("UploadErrorPartial", uploadViewModel);
}

xyz不为null但计数为零。但它在UploadErrorPartial.cshtml上显示了4行数据

我也尝试了ViewData:

更新 - ViewData仅将数据从控制器传输到视图,这就是为什么这不起作用。

if (Model.UploadExcelErrors != null)
{
    ViewData["xyz"] = Model;
    @Html.DevExpress().GridView(
        settings =>
        {
            settings.Name = "gridViewExcelError";
            settings.CallbackRouteValues = new { Controller = "PurchaseOrder", Action = "FilteringError" };
            settings.SettingsBehavior.AllowSort = false;
            settings.SettingsPager.Mode = GridViewPagerMode.ShowAllRecords;
            settings.Columns.Add(column => { column.FieldName = "rowNumber"; column.Caption = "Excel Row#"; });
            settings.Columns.Add(column => { column.FieldName = "errorMessage"; column.Caption = "Error Message"; });
            settings.Settings.ShowHeaderFilterButton = true;
            var headerFilterMode = GridHeaderFilterMode.List;
            foreach (GridViewDataColumn column in settings.Columns)
                column.SettingsHeaderFilter.Mode = headerFilterMode;
        }
    ).Bind(Model.UploadExcelErrors).GetHtml()
}

public ActionResult FilteringError()
{
    UploadViewModel uploadViewModel = (UploadViewModel)ViewData["xyz"];
    return PartialView("UploadErrorPartial", uploadViewModel);
}

uploadViewModel(UploadViewModel)ViewData["xyz"]获得null。

答案

我使用session来存储Model.UploadExcelErrors,它解决了我的问题。

以上是关于如何在回调期间将模型绑定发送到devexpress gridview回到控制器?的主要内容,如果未能解决你的问题,请参考以下文章

如何给DevExpress的LookUpEdit控件绑定多列数据

如何访问 DevExpress XtraTreeList 中绑定到节点的对象?

DevExpress MVC GridView 回调在第一次回调后冻结

Django信号

不要在焦点上隐藏错误

将套接字重新绑定到不同的接口