从DataBase MVC和Ajax Json Jtable中搜索记录

时间:2013-08-10 20:19:47

标签: visual-studio-2010 asp.net-mvc-3 jquery json.net jquery-jtable

几个小时后,我会尝试从数据库中搜索。我编写代码以便在您单击时从数据库中查找数据,但它不起作用。点击后没有任何改变,我想搜索一下例如“约翰”......有人帮我解释为什么会这样?

MyController.cs

using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project_MVC;

namespace Project_MVC.Controllers
{ 
    public class MyController : Controller
    {
        private mydatabase db = new mydatabase();


        [HttpPost]
        public JsonResult CheckData(string name = "",string cityid = "", int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        {
            try
            {

                //Get data from database
                using (var ls = new mydatabase())
                {
                    Wdata = ls.Procedure_ShowData(name,cityid).ToList<Procedure_ShowData_Result>();
                }



                //Return result to jTable
                return Json(new { Result = "OK", Records = Wdata });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = "No good" });
            }
        }

}
}

My.cshtml

<div>

        Name: <input type="text" name="name" id="name" />
       CityId: <input type="text" name="cityid" id="cityid" />
        <button type="submit" id="LoadRecordsButton">Load records</button>

</div>
<script>
    $('#Check').jtable({
            messages: polishMessages,
            paging: true, //Enable paging
            pageSize: 10,
            sorting: true,

            actions: {
                listAction: '@Url.Action("Check")'

            },
            fields: {
            Name: {
                    title: 'Name',
                    width: '20px'
                },
                CityId: {
                    title: 'CityId',
                    width: '20px'
                }
            }
        });
 $('#LoadRecordsButton').click(function (e) {
            e.preventDefault();
            $('#Check').jtable('load', {
                Name: $('#name').val(),
                CityId: $('#cityId').val()
            });
        });
        $('#LoadRecordsButton').click();

    });
</script>

1 个答案:

答案 0 :(得分:0)

您可能应首先将数据发布到Check Data方法,而不是仅在div中加载数据:

$('#LoadRecordsButton').click(function (e) {
            e.preventDefault();

$post('/MyController/CheckData?name='+$('#name').val()+'&cityid='+$('#cityId').val())
       .always(function()
           {
            $('#Check').jtable('load', {
                Name: $('#name').val(),
                CityId: $('#cityId').val()
             });
            });
        });