【问题标题】:Kendo ui AutoComplete with C# simple source带有 C# 简单源的 Kendo ui AutoComplete
【发布时间】:2012-12-12 03:47:35
【问题描述】:

我尝试使用带有 C# 数据源的 Kendo UI 自动完成工具。 在 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 文件或类似文件,您知道如何完成此操作吗?

我不想创建一个附有模型等的控制器...如果可以只用一页制作它,那就太好了。

【问题讨论】:

  • 为什么不使用asp.net网页,将所有的逻辑和视图呈现在同一个页面,或者使用asp.net网页表单?

标签: php asp.net razor autocomplete kendo-ui


【解决方案1】:

如果你使用 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

如果您使用网络表单,我会创建一个通用处理程序或网络服务(取决于您需要多少功能)

public class LoadStates : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        JavaScriptSerializer json = new JavaScriptSerializer();
        var data = GetData();
        context.Response.ContentType = "application/json";
        context.Response.Write(json.Serialize(new
        {
            data = data.Select(r => new
            {
                StateId = r.ID,
                StateName = r.Name
            })
        }));
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

为了完整起见.. 以下是使用 ashx 网络服务实现相同功能的方法

[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
            })
        };
    }
}

【讨论】:

  • 确实这将是 MVC 模式的解决方案,但我不使用 MVC,因此 Controller 不是已知类,如果我在 System.Web.Mvc 上添加引用 GetData 未知。
  • 上面提供的代码不是“完整的” GetData() 是一个您将编写的函数,它负责从您的数据库或其他任何地方检索对象
  • 我真丢脸。非常感谢您的回答。我将尝试学习如何将 Mvc 与 Kendo ui 一起使用。
猜你喜欢
  • 2014-10-05
  • 1970-01-01
  • 1970-01-01
  • 2012-09-15
  • 1970-01-01
  • 2014-05-30
  • 1970-01-01
  • 2013-11-25
  • 1970-01-01
相关资源
最近更新 更多