zoukankan      html  css  js  c++  java
  • Ajax 异步查询 ,刷新页面的一部分

    调用的过程是,通过Jquery注册单击事件,当单击分部视图中的按钮,就取得分部视图中文本框的值,然后调用$.Get()函数以Get形式调用控制器SearchPeople方法,参数为searchText=searhTerm,如果成功,返回一个匿名回调函数,返回结果更新#people-data元素。

    实际上也可以使用$("#people-data").load("SearchPeople",{ searchText: searchTerm })。但是jquery load()方法的传递方式是根据参数data来自动决定,如果没有参数传递,则采用get方式传递,如果有参数传递,则会自动转换为post。

    1、主视图:Index.cshtml

    @model IEnumerable<BootstrapMVC30Days.Models.Person>

    @{
    ViewBag.Title = "Index";
    }

    @{ Html.RenderPartial("PersonSearchForm");}    //插入分部视图@{Html.RenderPartial("PartialView");}
    <div id="people-data">
    @Html.DisplayForModel(Model)
    </div>

    @section scripts
    {
    <script type="text/javascript">
    $(function () {
    $("#search-btn").click(function () {
    var searchTerm = $("#search-text").val();
    $.get("SearchPeople", { searchText: searchTerm })
    .success(function (data) {
    $("#people-data").html(data);
    });
    });
    });
    </script>
    }

    2、分部视图 _PersonSearchForm.cshtml

    <hr />
    <div class="container">
    <div class="pull-right">
    <form class="form-inline" role="form">
    <div class="form-group">
    <label class="sr-only" for="search-text">Email Address</label>
    <input type="text" class="form-control" id="search-text" placeholder="请输入查询文字" />
    </div>
    <button type="button" class="btn btn-success" id="search-btn">查询</button>   //不能使用submit,也就是说不能使用<input type="submit"/>或<button type="submit" ></button> 。因为当点击鼠标的话,这样就相当于提交表单,访问Person主视图所在的控制器了。而文档框中没有值。也可以这样submit, 脚本这样写,

    $("#search-btn").click(function (e) {
    e.preventDefault();
    //....

    3、控制器方法 


    // GET: Person
    public ActionResult Index()
    {
    return View(_people);
    }

    ///返回部分视图

    public ActionResult SearchPeople(string searchText)
    {
    var term = searchText.ToLower();
    var result = _people
    .Where(p => p.FirstName.ToLower().Contains(term) ||
    p.LastName.ToLower().Contains(term));

    return PartialView("_SearchPeople", result);
    }

     .netFramework 和SQL 对于空值的处理是不一样的。.net返回所有行,SQL Server 或者EF 返回零行。

    the .NET Framework implementation of the Contains method returns all rows when you pass an empty string to it, but the Entity Framework
    provider for SQL Server Compact 4.0 returns zero rows for empty strings.

  • 相关阅读:
    tomcat调优
    使用Docker部署Spring Boot项目
    java配置ueditor中解决“未找到上传文件”错误提示
    java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
    spring01
    android中的wrap_content,match_parent, fill_parent
    RPC和http
    Failed to read artifact descriptor for xxx
    Error processing condition on org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration$ThymeleafWebMvcConfiguration.resourceUrlEncodingFilter
    springboot的自动配置
  • 原文地址:https://www.cnblogs.com/liuyuanhao/p/4379416.html
Copyright © 2011-2022 走看看