javascript - 为什么在 MVC 中从 ajax.post 调用时我的 View 没有渲染

标签 javascript ajax asp.net-mvc

因此,我使用 ViewResult 构建了一个 View ,该 View 有一个用于输入搜索字符串的文本框和一个用于提交的按钮。它工作得很好,它返回我搜索的项目并显示在屏幕上。现在,我的要求是将该文本框移动到主布局(如下)并从那里提交。但是,当我按下提交并调用 ajax post 时,调试显示搜索 viewResult 的搜索字符串,甚至到达关联的 View ,但 View 不会呈现。有任何想法吗?

这是搜索框

<div style="float:right;">
                <input type="text" id="searchString" name="searchString" />
                <a href="#" id="search" ><img src="~/Images/Custom/searchIcon.jpg" style="width:25px; height:25px;"/></a>
            </div>

这是带有 ajax 帖子的 javascript

<script>
    $(document).ready(function () {

        $("#search").click(function () {
            var searchString = $("#searchString").val();
            var datastring = { "searchString": searchString };

            $.ajax({

                url: "/Home/Search/",
                type: "POST",
                contentType: "application/json",
                data: JSON.stringify(datastring),
                datatype: 'json',
                success: function () {
                    console.log('success!!');
                }

            });
        });
    });
</script>

这是我的 ViewResult:

   public ViewResult Search(string searchString, int? pageNumber)
    {
      //  var searchString = fc["searchString"];
        var results = new ArrayList();
        var mylist = new List<SearchResult>();
        var model = new SearchViewModel();
        var host = "/CommunityWildlifeHabitat";
        if (System.Web.HttpContext.Current.Request.IsLocal)
            host = "";


            // Search Communities
            var search = from s in db.Communities
                         where s.ComunityName.Contains(searchString) || s.Description.Contains(searchString)
                         select s;

            // Search Resource Center
            var docs = from d in db.ResourceCenters
                       where d.Title.Contains(searchString) || d.Description.Contains(searchString)
                       select d;

        // Set up Arraylist with type Community
        foreach(var c in search)
        {
            var community = new SearchResult();
            community.type = "community";
            community.CommunityId = c.CommunityId;
            community.CommunityName = c.ComunityName;
            community.Description = c.Description;
            community.CommunityType = c.CommunityType1.TypeName;
            community.CommunityCity = c.CommunityCity;
            community.CommunityState = c.CommunityState;
            community.CommunityZip = c.CommunityZip;
            community.Population = c.Population;
            mylist.Add(community);
        }

        // Set up ArrayList with type ResourceCenter
        foreach (var d in docs)
        {
            var document = new SearchResult();
            document.type = "document";
            document.Title = d.Title;
            document.Document_Description = d.Description;
            document.FilePath = d.FilePath;
            document.Date = Convert.ToDateTime(d.Date);
            document.UpLoadedBy = d.UpLoadedBy;
            mylist.Add(document);
        }

        model.results = mylist;
        ViewBag.results = model.results;
        ViewBag.searchString = searchString;
        ViewBag.Host = host;

        return View(mylist.ToPagedList(pageNumber ?? 1, 10));
    }

最后,这是我的观点:

<h2>Search</h2>

@using (Html.BeginForm("Search", "Home", FormMethod.Get, new { @class = "form-horizontal", role = "form" }))
{
    @Html.TextBox("searchString", ViewBag.searchString as string, new { @class = "form-control", required = "required"})
    <input type="submit" class="btn btn-default" value="Search" />
    <hr />



    if (@Model.Count != 0)
    {
        <h3>The following results were found for @ViewBag.searchString</h3>


        foreach (var search in @Model)
        {

            if (@search.type == "community")
            {
                <div class="resource-element">
                    <a href="@ViewBag.Host/Communities/CommunityPage/@search.CommunityId">
                        <span class="resource-type pull-right">Community</span>
                    </a>
                    <a href="@ViewBag.Host/Communities/CommunityPage/@search.CommunityId"><h3>@search.CommunityName</h3></a>
                    <p>@search.Description</p>
                    <span class="">Type : @search.CommunityType</span><br />
                    <span class="">@search.CommunityCity, @search.CommunityState @search.CommunityZip</span><br />
                    <span class="">Population: @search.Population</span>
                    <br>
                </div>
            }
            else
            {


                <div class="resource-element">
                    <a href="@ViewBag.Host@search.FilePath">
                        <span class="resource-type pull-right">Document</span>
                    </a>
                    <a href="@ViewBag.Host@search.FilePath"><h3>@search.Title</h3></a>
                    <p>@search.Document_Description</p>
                    <span class="">@search.Date</span>
                    <br>
                    <span class="">@search.UpLoadedBy</span>
                    <br>
                </div>
            }

        }

        @Html.PagedListPager(Model, pageNumber => Url.Action("Search", "Home", new { searchString = @ViewBag.searchString, pageNumber }),
            new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = true })

    }
    else
    {
        if (@ViewBag.searchString != null)
        {
            <div class="resource-element">
                <a href="#">
                    <span class="resource-type pull-right"></span>
                </a>
                <h3>No Results Found</h3>
            </div>
        }

    }


}

最佳答案

如果您只是使用以下内容更改表单助手

@using (Html.BeginForm("Search", "Home", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{

并删除您的jquery,它应该将您的表单正确发布到服务器,并自动重新加载页面。

没有特定需要在页面内运行异步调用。

关于javascript - 为什么在 MVC 中从 ajax.post 调用时我的 View 没有渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43347077/

相关文章:

javascript - 在不使用溢出的情况下隐藏 HTML 页面上的默认滚动条 :hidden?

javascript - JS : how to access the variable declared in one JS file in another JS file

javascript - 如何将多个脚本合并为一个?

c# - 无法作为 href 访问条件 Razor 变量

asp.net - 有没有办法让盒式磁带捆绑 URL 永不过期

javascript - 如何删除json中不需要的编码符号

javascript - webpack 可以为浏览器和 Node 输出单独的脚本和模块文件吗?

javascript - 如何从 javascript 图表触发 ajax

javascript - 如何提醒ajax响应

javascript - 显示多个文件上传的进度 Jquery/Ajax