Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从Ajax.Post调用MVC视图而不返回调用页面_Javascript_Ajax_Asp.net Mvc - Fatal编程技术网

Javascript 如何从Ajax.Post调用MVC视图而不返回调用页面

Javascript 如何从Ajax.Post调用MVC视图而不返回调用页面,javascript,ajax,asp.net-mvc,Javascript,Ajax,Asp.net Mvc,我有一个使用JavaScript更新本地对象数组的网页,我想导航到另一个页面,传递对象数组,将其转换为一个列表,我使用Ajax.Post可以很好地工作,但是,我只希望返回视图(模型)像使用@Html.ActionScript或@Url.Action一样显示页面。但它返回到调用函数,并带有一个构建的页面 有没有一种方法可以将post函数调用为“fire and forget”类型的调用 或者有没有方法调用@Html.ActionScript或@UrlAction,将复杂数组作为参数传递 下面是我正

我有一个使用JavaScript更新本地对象数组的网页,我想导航到另一个页面,传递对象数组,将其转换为一个列表,我使用Ajax.Post可以很好地工作,但是,我只希望返回视图(模型)像使用@Html.ActionScript或@Url.Action一样显示页面。但它返回到调用函数,并带有一个构建的页面

有没有一种方法可以将post函数调用为“fire and forget”类型的调用

或者有没有方法调用@Html.ActionScript或@UrlAction,将复杂数组作为参数传递

下面是我正在使用的一些代码

JS

控制器

        [HttpPost]
    public ActionResult About(List<ClientDataModels> clients)
    {
        //Do something, all i need to do is pass the clients
        //to the view, and then dynamically add items based
        //on the content

        return View(clients);
    }
[HttpPost]
公共操作结果关于(列出客户端)
{
//做点什么,我要做的就是通过客户
//添加到视图中,然后根据需要动态添加项目
//论内容
返回视图(客户端);
}
关于.cshtml

    @using SST.Monitoring.Models
@model IEnumerable<ClientDataModels>
@{
    //Layout = null;
    ViewBag.Title = "About";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

<div class="container" style="width:900px">
    @if(@Model != null)
    { 
        foreach ( var client in @Model )
        {
            <div style="width:150px; float:left;">
                <div class="tile-container">
                    <div class="tile bg-cyan fg-white" data-role="tile">
                        <div class="tile-content iconic">
                            <span class="icon mif-display mif-2x"></span>
                            <span id="ClientCount" class="tile-badge bg-darkBlue">0</span>
                            <span class="tile-label">client.ClientGroup</span>
                        </div>
                    </div>
                </div>
            </div>
        }
    }
</div>
@使用SST.Monitoring.Models
@模型IEnumerable
@{
//布局=空;
ViewBag.Title=“关于”;
}
@ViewBag.Title。
@查看包。留言
@如果(@Model!=null)
{ 
foreach(@Model中的var客户端)
{
0
client.ClientGroup
}
}

返回到ajax调用的页面包含我期望的3个项目(平铺),但我希望它只是触发并忘记,并让视图以@html.actionscript或event just a href link?的方式显示,因为我知道您希望实现类似的效果

您可以有一种在控制器内渲染视图的方法

public static string RenderPartialView(this Controller controller, 
        string viewName, object model)
    {
        if (string.IsNullOrEmpty(viewName))
            viewName = controller.ControllerContext.RouteData
                .GetRequiredString("action");

        controller.ViewData.Model = model;
        using (var sw = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines
                .FindPartialView(controller.ControllerContext, viewName);
            var viewContext = new ViewContext(controller.ControllerContext, 
                viewResult.View, controller.ViewData, controller.TempData, sw);
            viewResult.View.Render(viewContext, sw);

            return sw.GetStringBuilder().ToString();
        }
    }
此方法将创建所需的html,就像返回视图一样

您必须将此html添加到响应(json响应)中

当您收到ajax响应时,您将得到html,只需
empty()
您的容器,并
将(response.HtmlFromRenderPartialView)
附加到您的容器中

更新:

    [HttpPost]
    public About(List<ClientDataModels> clients)
    {
        //Do something, all i need to do is pass the clients
        //to the view, and then dynamically add items based
        //on the content
      return Json(new
        {
    HtmlFromRenderPartialView = this.RenderPartialView("_About", clients),
        }, JsonRequestBehavior.AllowGet);
    }



$.ajax({
    url: "/Home/About",
    type: "POST",
    data: JSON.stringify({ 'clients': clients }),
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    traditional: true,
    success: function (response) {
       $('#container').html(response.HtmlFromRenderPartialView );
    },
[HttpPost]
公共关于(列出客户)
{
//做点什么,我要做的就是通过客户
//添加到视图中,然后根据需要动态添加项目
//论内容
返回Json(新的
{
HtmlFromRenderPartialView=this.RenderPartialView(“关于”,客户端),
},JsonRequestBehavior.AllowGet);
}
$.ajax({
url:“/Home/About”,
类型:“POST”,
数据:JSON.stringify({'clients':clients}),
数据类型:“json”,
contentType:“应用程序/json;字符集=utf-8”,
传统的:是的,
成功:功能(响应){
$('#container').html(response.HtmlFromRenderPartialView);
},

好的,基于我得到了渲染页面的结果,我刚刚将其加载到当前窗口中

        var w = window.open("","_self");
        $(w.document.body).html(data.responseText);

这似乎成功了…谢谢你的帮助!!

嗨,谢谢你的回复,我确实从ajax.post调用中得到了一个返回值,这是一条成功消息,我可以将消息视为html,它显示了我所期望的内容,一个html页面,事件(如果没有应用样式)。我猜你上面的建议会再次出现n当ajax调用返回时,我仍然需要处理的部分视图?我希望我可以调用@Url.Action并将viewmodel列表作为参数传递?我不知道如何做您想要的事情,我认为它不存在。我已经根据我最初的想法和您的更新更新了我的答案。啊,好的,所以我基本上只是创建一个使用#容器创建局部视图,然后以这种方式更新?
    [HttpPost]
    public About(List<ClientDataModels> clients)
    {
        //Do something, all i need to do is pass the clients
        //to the view, and then dynamically add items based
        //on the content
      return Json(new
        {
    HtmlFromRenderPartialView = this.RenderPartialView("_About", clients),
        }, JsonRequestBehavior.AllowGet);
    }



$.ajax({
    url: "/Home/About",
    type: "POST",
    data: JSON.stringify({ 'clients': clients }),
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    traditional: true,
    success: function (response) {
       $('#container').html(response.HtmlFromRenderPartialView );
    },
        var w = window.open("","_self");
        $(w.document.body).html(data.responseText);