简体   繁体   English

在MVC 4中加载ajax成功方法的部分视图

[英]Load Partial View on ajax success method in MVC 4

Problem Statement :View not able to load on ajax success method. 问题陈述 :查看无法加载ajax成功方法。

Description : I'm having couple of dropdowns as cascaded(second binding based on the value of first). 描述 :我有几个下拉级联(基于第一个值的第二个绑定)。 On change of the first dropdown I'm binding second dropdown and again on change of the second dropdown,now on right side I want to display a list of record based on the two dropdown values. 在更改第一个下拉列表时,我将绑定第二个下拉列表并再次更改第二个下拉列表,现在在右侧,我想显示基于两个下拉列表值的记录列表。

For this,I'm using the following JS code in onchange event for the dropdown 2: 为此,我在下拉列表2的onchange事件中使用以下JS代码:

function ShowDocsList() {
        var teamId = $('#TeamID').val();
        var projectId = $("#ProjectID").val();
        var Url = "@Url.Content("~/DocsHome/DocsList")";
        $.ajax({
            url: Url,
            type:'POST',
            dataType: 'html',
            data: { TeamID: teamId ,ProjectID : projectId},
            success: function (data) {
            return data;
              $('.docs-detail').html(data);
            }
        });

Here,in DocsHome Controller,DocsList method is getting hit on change of second dropdown which is project dropdown.But the view is not getting rendered .Following is my Controller Code: 在DocsHome控制器中,DocsList方法在第二个下拉列表的更改中受到影响,这是项目下拉列表。但是视图没有被渲染。以下是我的控制器代码:

 public ActionResult DocsList(int teamId, int projectId)
        {
            List<CustomerViewModel> customerViewsModels = SmartAdminHelper.GetCustomers(db1);
            if (Request.IsAjaxRequest())
                return PartialView("DocsList");
            else
                return View("DocsList");

        }

Again ,I'm getting record in List but while debugging it does not pass to the DocsList view which according to me is correct. 再次,我在List中获取记录但是在调试时它没有传递到DocsList视图,根据我的说法是正确的。

Here DocsList is the view I want to render as a partial view on change of the second dropdown. 这里DocsList是我想要渲染的视图,作为第二个下拉列表更改的局部视图。 According to my knowledge,while debugging it comes to the point return PartialView("DocsList") but then again it goes back to the ajax success method and finally I find that there I'm doing something wrong. 根据我的知识,虽然调试它返回到返回PartialView(“DocsList”)的点,然后再次回到ajax成功方法,最后我发现我做错了。

Earlier I have Json to get data but here I'm calling actionmethod from ajax. 早些时候我让Json获取数据,但在这里我从ajax调用action方法。 So, not sure that also might be a problem as I'm new to this. 所以,不确定这也可能是一个问题因为我是新手。

What and where exactly I'm doing wrong? 究竟我做错了什么?

Saroj, I see that this is an old question and that you needed to get it done quickly, but if you happend to come back to this, I'll add my two cents. Saroj,我看到这是一个老问题,你需要快速完成它,但如果你回到这里,我会加上我的两分钱。 You need to remove the return statement that David and Ehsan mention above. 您需要删除David和Ehsan在上面提到的return语句。 The rest of the callback function does what it should. 回调函数的其余部分完成了应有的功能。 In your action method it doesn't look like you're doing anything with the parameters you pass in. I'm assuming that you are going to figure that out after you get the view down to the client. 在你的动作方法中,你看起来并没有对你传入的参数做任何事情。我假设在你将视图传递给客户端后你会想到这一点。 So, lets get that view down to the client. 所以,让我们将该视图下载到客户端。

I like to pass the rendered partial view back to the client as a string of HTML. 我喜欢将呈现的局部视图作为HTML字符串传递回客户端。 I do this using a method that I keep in a controller base class that each of my controllers inherit from. 我使用一种方法来执行此操作,该方法保存在每个控制器继承的控制器基类中。 To use the method you will need to reference System.Web.Mvc and System.IO namespaces. 要使用该方法,您需要引用System.Web.Mvc和System.IO名称空间。

The method looks like this: 该方法如下所示:

private string RenderViewToString( string viewName, object model ) {
            ViewData.Model = model;
            using ( var sw = new StringWriter() ) {
                var viewResult = ViewEngines.Engines.FindPartialView( ControllerContext, viewName );
                var viewContext = new ViewContext( ControllerContext, viewResult.View, ViewData, TempData, sw );
                viewResult.View.Render( viewContext, sw );
                viewResult.ViewEngine.ReleaseView( ControllerContext, viewResult.View );
                return sw.GetStringBuilder().ToString();
            }
        }

You pass your model and the name of the view to the method and it returns the rendered view HTML as a string which you can return to the client as a ContentResult. 将模型和视图名称传递给方法,它将呈现的视图HTML作为字符串返回,您可以将其作为ContentResult返回给客户端。

Update your action method like so: 像这样更新你的行动方法:

 public ActionResult DocsList(int teamId, int projectId)
            {
                List<CustomerViewModel> customerViewsModels = SmartAdminHelper.GetCustomers(db1);
                if (Request.IsAjaxRequest())
                    var viewContents = RenderViewToString("DocsList", customerViewsModels);
                    return Content(viewContents);
                else
                    return View("DocsList");

            }

Assuming that the element that you want the rendered view to appear in has the css class '.docs-detail' on it you'll be in business. 假设您希望渲染视图出现的元素上有css类'.docs-detail',那么您将开始营业。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM