ASP.NET MVC jQGrid客户端和服务器端验证

时间:2011-06-07 11:41:04

标签: asp.net-mvc-3 jqgrid-asp.net

我无法找到如何使用MVC jQGrid进行客户端验证的任何示例。 对于客户端,我可以看到我需要分配列属性EditClientSideValidators

public List<JQGridEditClientSideValidator> EditClientSideValidators { get; set; }

但是我无法理解如何为其分配验证。

但如何进行服务器验证也是一个问题

更新

我想我找到了一些客户端验证http://www.trirand.net/aspnetmvc/grid/editclientsidevalidation

1 个答案:

答案 0 :(得分:0)

如果您获得了包含支持的许可证,您可以询问开发人员...否则,您显示的链接具有文档......网格需要在获取数据时请求验证器,如下所示:

public void SetUpGrid_EditClientSideValidation(JQGrid grid)
{
    // Setting the DataUrl to an action (method) in the controller is required.
    // This action will return the data needed by the grid.
    // EditUrl will point to the editing action
    grid.DataUrl = Url.Action("EditClientSideValidation_DataRequested");
    grid.EditUrl = Url.Action("EditClientSideValidation_EditRow");

    var integerColumn = grid.Columns.Find(c => c.DataField == "Integer");
    integerColumn.EditClientSideValidators.Add(new RequiredValidator());
    integerColumn.EditClientSideValidators.Add(new MinValueValidator { MinValue = 10 });
    integerColumn.EditClientSideValidators.Add(new MaxValueValidator { MaxValue = 100 });

    var numberColumn = grid.Columns.Find(c => c.DataField == "Number");
    numberColumn.EditClientSideValidators.Add(new RequiredValidator());
    numberColumn.EditClientSideValidators.Add(new MinValueValidator { MinValue = 0.1 });
    numberColumn.EditClientSideValidators.Add(new MaxValueValidator { MaxValue = 100.1 });

    var emailColumn = grid.Columns.Find(c => c.DataField == "Email");
    emailColumn.EditClientSideValidators.Add(new RequiredValidator());
    emailColumn.EditClientSideValidators.Add(new EmailValidator());

    var linkColumn = grid.Columns.Find(c => c.DataField == "Link");
    linkColumn.EditClientSideValidators.Add(new RequiredValidator());
    linkColumn.EditClientSideValidators.Add(new UrlValidator());

    var customColumn = grid.Columns.Find(c => c.DataField == "Custom");
    customColumn.EditClientSideValidators.Add(new Trirand.Web.Mvc.CustomValidator { ValidationFunction = "validateCustomField" });
    }

// This method is called when the grid requests data. You can choose any method to call
// by setting the JQGrid.DataUrl property
public JsonResult EditClientSideValidation_DataRequested()
{
    // Get both the grid Model
    // The data model in our case is an autogenerated linq2sql database based on Northwind.
    var gridModel = new EditValidationModel();
    SetUpGrid_EditClientSideValidation(gridModel.ValidatedGrid);

    // return the result of the DataBind method, passing the datasource as a parameter
    // jqGrid for ASP.NET MVC automatically takes care of paging, sorting, filtering/searching, etc
    List employees = GetClientValidatedEmployeeData();
    return gridModel.ValidatedGrid.DataBind(employees.AsQueryable());
}