如何突出显示kendo网格中的重复值

时间:2018-05-10 06:29:58

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

这是我防止控制器中重复值的逻辑

public ActionResult ProviderType_Create([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<ProviderTypeMasterViewModel> ProviderTypeMasterList)
    {
        var results = new List<ProviderTypeMasterViewModel>();
        try
        {
            _logger.LogInformation("ProviderTypeMastersController ProviderType_Create Start");
            foreach (var ProviderTypeMaster in ProviderTypeMasterList)
            {
                TblProviderTypeMaster ptm = new ProviderTypeMasterViewModel().ToModel(ProviderTypeMaster);
                var provd = _context.TblProviderTypeMasters.Where(p => p.ProviderTypeName == ProviderTypeMaster.ProviderTypeName).ToList();
                if (provd != null && provd.Count() == 0)
                {
                    if (ProviderTypeMasterList != null && ModelState.IsValid)
                    {
                        string userID = GetUserID();
                        providerTypeMasterService.SaveProviderTypeMaster(ProviderTypeMaster, userID);
                    }
                }
                else
                {
                    duplicate = true;
                    return this.Json(new DataSourceResult
                    {
                        Errors = "my custom error"
                    });
                }
            }

            _logger.LogInformation("ProviderTypeMastersController ProviderType_Create Complete");
        }
        catch (Exception e)
        {
            _logger.LogError("ProviderTypeMastersController ProviderType_Create Failed - " + e.Message);
        }
        return Json(results.ToDataSourceResult(request, ModelState));
    }

我使用视图中的kendo网格错误事件在警告消息中显示错误。现在我需要帮助这两件事。

1。)是否有其他方法可以在没有警报消息的情况下显示错误消息。像标签一样?如果是这样,在删除副本后我应该在哪里隐藏标签?

2.)我想突出显示用户输入重复值的网格的特定值。当id重复时将更改特定网格值更改为红色的更改,并在用户将重复值更改为唯一值时删除红色。

我是初学者,我被困在这里。谁能帮我这个?感谢

2 个答案:

答案 0 :(得分:0)

只需将错误添加到ModelState:

public JsonResult Create([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<ProviderTypeMasterViewModel> ProviderTypeMasterList)
{
    if (ModelState.IsValid)
    {
        foreach (var ProviderTypeMaster in ProviderTypeMasterList)
        {
            TblProviderTypeMaster ptm = new ProviderTypeMasterViewModel().ToModel(ProviderTypeMaster);
            if (_context.TblProviderTypeMasters.Any(p => p.ProviderTypeName == ProviderTypeMaster.ProviderTypeName))
            {
                 ModelState.AddModelError("ProviderTypeName", "ProviderType already exists");
            }
            else
            {
                if (ProviderTypeMasterList != null)
                {
                    string userID = GetUserID();
                    providerTypeMasterService.SaveProviderTypeMaster(ProviderTypeMaster, userID);
                }
            }
        }
    }
}
return Json(results.ToDataSourceResult(request, ModelState));

答案 1 :(得分:0)

为了使用条件选择的操作格式化Kendo Grid Column值,您可以使用下面的一个合适示例。有关更多信息:How Do I Have Conditional Logic in a Column Client Template?

Javascript的用户界面:

{
    field: "EmployeeName", type: "string", width: "55px", title: "Employee Name", 
       template: "#= GetEditTemplate(data) #"
}


MVC的 用户界面:

...
columns.Bound(t => t.EmployeeName).Title("Status Name").Template(@<text></text>)
    .ClientTemplate("#= GetEditTemplate(data)#").Width("55px");
...

以下是示例中使用的方法:

<script>
//Change the color of the cell value according to the given condition
function GetEditTemplate(data) {
    var html;

    if (data.StatusID == 1) {
        html = kendo.format(
        //"<a class=\"k-button\" href='" + '@Url.Action("Edit1", "Controller")' + "/{0}" + " '>Edit</a>  ",
        "<span class='text-success'>" +
        data.EmployeeName
        + "</span>"
        );
    }
    else {
        html = kendo.format(
        //"<a class=\"k-button\" href='" + '@Url.Action("Edit2", "Controller")' + "/{0}" + " '>Edit</a>  ",
        "<span class='text-danger'>Cancel</span>"
        );
    }
    return html;
}
</script>

希望这会有所帮助......