Php 剑道ui自动完成与C#简单源代码

Php 剑道ui自动完成与C#简单源代码,php,asp.net,razor,autocomplete,kendo-ui,Php,Asp.net,Razor,Autocomplete,Kendo Ui,我尝试将剑道UI自动完成工具与C#数据源一起使用。 在PHP中似乎很容易: <?php include("connection.php"); $arr = array(); $stmt = $db->prepare("SELECT StateID, StateName FROM USStates WHERE StateName LIKE ?"); // get the StartsWith value and append a %

我尝试将剑道UI自动完成工具与C#数据源一起使用。 在PHP中似乎很容易:

    <?php
        include("connection.php");

    $arr = array();

    $stmt = $db->prepare("SELECT StateID, StateName FROM USStates WHERE StateName LIKE ?");

    // get the StartsWith value and append a % wildcard on the end
    if ($stmt->execute(array($_GET["StartsWith"]. "%"))) {
        while ($row = $stmt->fetch()) {
            $arr[] = $row;    
        }
    }

    // add the header line to specify that the content type is JSON
    header("Content-type: application/json");

    echo "{\"data\":" .json_encode($arr). "}";
?>

但是我想使用CSHtml文件或类似的东西,你知道如何做到这一点吗


我不想创建一个附加了模型的控制器,等等。。。如果只需要一个页面就可以了,那就太好了。

如果您使用的是MVC,那么创建一个这样的控制器

public class DataController : Controller
{
    public JsonResult GetStates()
    {
        var data = GetData();
        return Json(new
        {
            data = data.Select(r => new
            {
                StateId = r.ID,
                StateName = r.Name
            })
        });
    }
 }
然后,您所要做的就是将数据源url设置为/data/GetStates

如果您使用的是webforms,我会创建一个通用处理程序或webservice(取决于您需要多少函数)

为了完整起见。。下面是如何使用ashx Web服务实现同样的功能

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService1 : System.Web.Services.WebService
{
    [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string HelloWorld()
    {
        var data = GetData();
        return new
        {
            data = data.Select(r => new
            {
                StateId = r.ID,
                StateName = r.Name
            })
        };
    }
}

为什么不使用asp.net网页(在同一页面中具有所有逻辑和视图呈现)或使用asp.net web表单?确实,这将是一个具有MVC模式的解决方案,但我不使用MVC,因此如果我在System.web上添加引用,Controller不是已知的类。MVC GetData未知。上面提供的代码不是“完整的”GetData()是一个你将要编写的函数,它将负责从你的数据库中检索对象,或者其他让我感到羞耻的事情。非常感谢你的回答。我将尝试学习如何将Mvc与剑道ui结合使用。
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService1 : System.Web.Services.WebService
{
    [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string HelloWorld()
    {
        var data = GetData();
        return new
        {
            data = data.Select(r => new
            {
                StateId = r.ID,
                StateName = r.Name
            })
        };
    }
}