asp.net mvc Ajax.BeginForm复制元素

时间:2018-11-18 17:31:28

标签: javascript c# asp.net-mvc asp.net-mvc-4 ajax.beginform

我正在使用Ajax通过部分视图加载数据,并且同时实现了搜索功能。它的工作正常,但是当我将搜索文本框保留为空然后单击搜索按钮时,我的布局就会重复。
这是我的代码

控制器

     public async Task<ActionResult> Index(string search)
     {
        if (!Request.IsAjaxRequest())
            return View(await _employeeRepository.GetAllEmployeesAsync());

        if (string.IsNullOrEmpty(search))
            return View(await _employeeRepository.GetAllEmployeesAsync());
        return PartialView("_EmployeeList",(await _employeeRepository
            .GetAllEmployeesAsync())
            .Where(x =>x.EmployeeName.StartsWith(search,
                    StringComparison.OrdinalIgnoreCase)));
     }


Index.cshtml

    @model IEnumerable<MvcDemo.Models.Employee>


   @using (Ajax.BeginForm("Index", "Employee", new AjaxOptions
   {
       OnFailure = "record not loaded",
       HttpMethod = "GET",
       UpdateTargetId = "employeeData",
       InsertionMode = InsertionMode.Replace
   }))
   {
      <input type="text" name="search"
        class="form-control"
        placeholder="search by name..."/>

      <input type="submit" value="Search"/>
    }
    <div id="employeeData">
        @Html.Partial("_EmployeeList", Model);
        @* i have used render partial as well*@
    </div>

   @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
      @Scripts.Render("~/bundles/jqueryajax")
   }


BundleConfig

    bundles.Add(new ScriptBundle("~/bundles/jqueryajax").Include(
            "~/Scripts/jquery.unobtrusive-ajax.min.js"));


Layout.cshtml

    @Scripts.Render("~/bundles/jqueryajax")


有人可以告诉我我要去哪里哪里吗?
将输入保留为空白,然后单击搜索按钮来复制布局。
看看我的屏幕截图
screen 1
screen 2
screen 3
任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果搜索文本为空或为空,则返回的是索引视图,而不是返回的部分视图。

if (string.IsNullOrEmpty(search))
    return PartialView("_EmployeeList", (await _employeeRepository.GetAllEmployeesAsync()));