Javascript 为什么在MVC中从ajax.post调用时我的视图不呈现

Javascript 为什么在MVC中从ajax.post调用时我的视图不呈现,javascript,ajax,asp.net-mvc,Javascript,Ajax,Asp.net Mvc,因此,我构建了一个带有ViewResult的视图,该视图有一个文本框来输入搜索字符串和一个按钮来提交。它工作得很好,它返回我搜索的项目并显示在屏幕上。现在,我的要求是将该文本框移动到下面的主布局并从那里提交。但是,当我按下submit键并调用ajax post时,调试显示搜索viewResult的搜索字符串,它甚至可以访问关联的视图,但视图没有呈现。有什么想法吗 这是搜索框的名称 <div style="float:right;"> <input

因此,我构建了一个带有ViewResult的视图,该视图有一个文本框来输入搜索字符串和一个按钮来提交。它工作得很好,它返回我搜索的项目并显示在屏幕上。现在,我的要求是将该文本框移动到下面的主布局并从那里提交。但是,当我按下submit键并调用ajax post时,调试显示搜索viewResult的搜索字符串,它甚至可以访问关联的视图,但视图没有呈现。有什么想法吗

这是搜索框的名称

<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>
下面是我的查看结果:

   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,它会将表单正确地发布到服务器,并自动重新加载页面


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

我认为您必须手动触发渲染。因此,尝试将success:function{console.log'success!!';}替换为可以呈现视图的js函数。如果IDE中不存在这样的函数,则必须手动将其插入DOM。是的,我已经考虑过了。谢谢如果你把它标记为C mvc或ASP mvc,我猜这是C,但不是100%确定你也能从那里得到答案。这里的一切似乎都很正常,除了成功后你只是简单地向控制台写信-是否有一个结果应该呈现到的特定div?