搜索时填充Kendo网格

时间:2013-11-05 14:46:17

标签: asp.net-mvc jquery asp.net-mvc-4

我需要从搜索按钮填充Kendo网格。我写了个人ID,我应该在网格中获取其他信息,但在文本框中写入ID并点击搜索后,我得到了

  

“服务器无法处理请求.--->程序或功能   'PROS_CRA_PUBLIC_GET_DOCUMENT_LIST_BY_PN'需要参数'@PN',   这是没有提供的。“

控制器

public ActionResult Search(string personalID, [DataSourceRequest]DataSourceRequest request)
    {



        var serviceclient = new PersonalInfoServiceClient();
        DataSourceResult result = null;
        try
        {
            var items = serviceclient.GetRegistryInfoForPerson(personalID);
            result = items.ToDataSourceResult(request);

        }
        catch 
        {
            throw;
        }
        finally
        {
            serviceclient.Close();
        }

        return Json(result, JsonRequestBehavior.AllowGet);

    }

查看(部分内容)

 <div id ="search">
@using (Html.BeginForm("Search", "Main")){    
     <p> personal number: <input type="text" name="Searchtext" id="SearchString" /> <input type="submit" value="search" name="btnSearch" id="btnSearch" /></p> 
    }

<div id="civil">
@(Html.Kendo().Grid<PersonalIDCheckerMvCKendo.Models.PersInfo>()   
.Name("Grid")
.AutoBind(false)
.Columns(columns =>
{
    //columns//    })
.Groupable()
.Sortable()
.Scrollable()   
.Filterable()
.DataSource(dataSource => dataSource
    .Ajax()
    .Model(model => model.Id(c=> c.personalID))
    .Read(read => read.Action("Search", "Main").Data("test"))
)
)
</div>

<script>
$("#btnSearch").on('click', function test() {

console.log("click");
var searchText = $("#SearchString").val();

if (searchText == "") {

alert("You must enter a search value");
return;
}

$.ajax({
    url: '@Url.Action("Search","Main")',
    data: { personalID: searchText},
    type: 'POST',
    dataType: "json",
    success: function(result) {
var grid = $('#Grid').getKendoGrid(); 
grid.dataSource.data(result);
grid.refresh();
        }
    });
});
    </script>

模型

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ServiceModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using DataAnnotationsExtensions;

namespace PersonalIDCheckerMvCKendo.Models
{
[DataContract(IsReference=true)]
public class PersInfo
{
    [DataMember]
    [DisplayName("Personal#")]
    public string personalID
    {
        get
        {
            return _PersonalID;
        }

        set
        {
            if (_PersonalID != value)
            {
                _PersonalID = value;
            }
        }
    }
    private string _PersonalID;

    [DataMember]
    [DisplayName("Name")]
    public string FirstName
    {
        get
        {
            return _FirstName;
        }

        set
        {
            if (_FirstName != value)
            {
                _FirstName = value;
            }
        }
    }
    private string _FirstName;

    [DataMember]
    [DisplayName("Lastname")]
    public string LastName
    {
        get
        {
            return _LastName;
        }

        set
        {
            if (_LastName != value)
            {
                _LastName = value;
            }
        }
    }
    private string _LastName;

//etc

谢谢

2 个答案:

答案 0 :(得分:1)

您没有传递 personalID

添加一个javascript函数,负责传递控制器函数所需的数据,例如

function additionalData() {
    return {
        personalID: '@Model.personalID'
    };
}

然后更改网格的DataSource以使用javascript函数

.DataSource(dataSource => dataSource.Ajax().ServerOperation(false)
    .Read(read => read.Action("Search", "Main")                                                                                      
    .Data("additionalData"))
)

还要确保

  

[DataSourceRequest] DataSourceRequest请求

参数首先放在控制器功能中,例如

public ActionResult Search([DataSourceRequest]DataSourceRequest request
                           , string personalID)

还要确保使用返回的对象的属性绑定网格的列(在您的情况下为PersonalIDCheckerMvCKendo.Models.PersInfo)。例如

.Columns(columns =>
     {
          columns.Bound(pi => pi.Name);
          columns.Bound(pi => pi.Surname);
          // Some other columns.                                    
     })

*的更新

为了帮助您,我添加了一个工作网格示例。这是我的网格

@(Html.Kendo().Grid<ClientSearchResult>()
    .Name("Grid")
    .HtmlAttributes(new {style = "height:400px; cursor:pointer"})
    .DataSource(dataSource => dataSource.Ajax().ServerOperation(false)
        .Read(read => read.Action("RefreshGrid", "Client")                                                                                                 
        .Data("additionalData"))
        .PageSize(20)
    )
    .Columns(columns =>
    {
        columns.Bound(c => c.ClientId);
        columns.Bound(c => c.Name);
        columns.Bound(c => c.PolicyNumber);
        columns.Bound(c => c.ClaimNumber);
        columns.Bound(c => c.Status);
        columns.Bound(c => c.IsCompany);
    })
    .Pageable(page => page.Enabled(true).PageSizes(new[] {10, 20, 30, 40}))
    .Sortable(sorting => sorting.SortMode(GridSortMode.SingleColumn))
    .Scrollable()
    .Selectable(selectable => selectable.Mode(GridSelectionMode.Single))
    .Events(events => events.Change("grid_selected"))
)

我的javascript函数

function additionalData() {
    return {
        cellNumber: '@Model.CellNumber',
        name: '@Model.Name',
        policyNumber: '@Model.PolicyNumber',
        claimNumber: '@Model.ClaimNumber',
        brokerCode: '@Model.BrokerCode',
        brokerName: '@Model.BrokerName'
    };
}

和我的控制器功能

public ActionResult RefreshGrid([DataSourceRequest] DataSourceRequest request
                                , string cellNumber, string name 
                                , string policyNumber, string claimNumber
                                , string brokerCode, string brokerName)
{
    if (cellNumber == string.Empty && name == string.Empty && policyNumber ==   
        string.Empty && claimNumber == string.Empty && brokerCode == string.Empty &&   
        brokerName == string.Empty)
            return Json(new List<ClientSearchResult>());
        else
        {
            List<ClientSearchResult> searchResult =   
            ClientMaintenance.ClientSearch(cellNumber, name, policyNumber, claimNumber,        
                                           brokerCode, brokerName).ToList();
            var result = searchResult.ToDataSourceResult(request);
            return Json(result);
        }
}

答案 1 :(得分:0)

我修好了

将控制器更改为:

    public ActionResult Search([DataSourceRequest]DataSourceRequest request, string searchString)
    {



        var serviceclient = new PersonalInfoServiceClient();
        DataSourceResult result = null;
        if (!String.IsNullOrEmpty(searchString))
        {
            try
            {
                var items = serviceclient.GetRegistryInfoForPerson(searchString);
                result = items.ToDataSourceResult(request);

            }

            catch
            {
                throw;
            }

            finally
            {
                serviceclient.Close();
            }
        }

        return Json(result, JsonRequestBehavior.AllowGet);

    }

查看:

文本框:

@Html.TextBox("SearchString")

功能:

<script type="text/javascript">
$(document).ready(function(){
    $("#btnSearch").click(function (e) {
     $.ajax({
            url: '@Url.Action("Search","Main")',
            type: 'POST',
            dataType: "json",
            success: function (result) {    

                $("#Grid").data("kendoGrid").dataSource.read();
                $("#Grid").data("kendoGrid").refresh();
                e.preventDefault();
            }