简体   繁体   English

MVC Ajax部分视图-根据返回的视图加载到不同的div中

[英]MVC Ajax partial view - load into different div depending on which view is returned

Is there a way to set which div the partial view is loaded into depending on which view is returned from the MVC action? 有没有一种方法可以设置部分视图加载到哪个div中,具体取决于从MVC操作返回哪个视图? For example an MVC controller action could return one of two views (View A or View B) depending on some logic. 例如,根据某些逻辑,MVC控制器操作可以返回两个视图之一(视图A或视图B)。 If 'View A' is returned then in the done function of the Ajax call load it into 'div A', but if 'View B' is returned then I load it into 'div B'. 如果返回“视图A”,则在Ajax调用的完成函数中将其加载到“ div A”中,但是如果返回“视图B”,则将其加载到“ div B”中。

public ActionResult SomeActionMethod()
{
    if (someCondition == true) {
        return PartialView("ViewA", new SomeModel());
    }
    else {
        return PartialView("ViewB", new AnotherModel());
    }
}
.done(function(data) {
    if (isViewA === true) { // How do I determine which view it is?
        $('#div-a').html(data);
    } else {
        $('#div-b').html(data);
    }
})

Is there an known/accepted way to do this (how to determine if it's View A or View B)? 是否存在已知/可接受的方法(如何确定是View A还是View B)? I've thought of two ways to do this but both seem a little hacky. 我想到了两种方法来做,但是似乎都有些怪异。 If there is no commonly accepted/approved way of doing this then are the following ideas okay... 如果没有普遍接受/批准的方法,那么以下想法可以吗...

Idea 1: 想法1:

Search returned html for a unique element or hidden input value only found in that view. 在返回的html中搜索仅在该视图中找到的唯一元素或隐藏的输入值。 This isn't as easy as it first sounds since the returned data isn't just simple html but some sort of nested string/object (not really sure). 由于返回的数据不仅仅是简单的html,而是某种嵌套的字符串/对象(不确定),所以这并不像听起来那么简单。 For top level elements the jQuery 'filter' method needs to be used: 对于顶级元素,需要使用jQuery'filter'方法:

$(data).filter('#unique-element');

Otherwise the 'find' method should be used. 否则,应使用“查找”方法。

Idea 2: 想法2:

In the controller action, for one of the views being returned (View A), set Response.StatusCode to a custom value in the 200's range that isn't already used (not 200-208, 226). 在控制器操作中,对于要返回的一个视图(视图A),将Response.StatusCode设置为200范围内尚未使用的自定义值(不是200-208、226)。 Then in the ajax done function specify all 3 parameters and check the third parameters status property to compare to the response status you set in the controller action. 然后,在ajax done函数中,指定所有3个参数并检查第三个参数status属性,以与您在控制器操作中设置的响应状态进行比较。

if (someCondition == true) {
    Response.StatusCode = 299;
    return PartialView("ViewA", new SomeModel());
}
else {
    return PartialView("ViewB", new AnotherModel());
}
.done(function(data, textStatus, jqXHR) {
    if (jqXHR.status === 299) {
        // It’s View A so load it into div A
    }
    else {
        // It’s View B so load it into div B
    }
})

This works like a charm but seems even more hacky than Idea 1. 这就像一个魅力,但似乎比Idea 1更加顽强。

I would suggest loading the content of each div separately. 我建议分别加载每个div的内容。 Eg: 例如:

Javascript 使用Javascript

$('div-a').html(contentforA);
$('div-b').html(contentforB);

Controller 调节器

public ActionResult ContentA()
{
    if (someCondition)
    {
        return PartialView("ViewA", new SomeModel());
    }
    return new EmptyResult();
}

public ActionResult ContentB()
{
    if (!someCondition)
    {
        return PartialView("ViewB", new AnotherModel());
    }
    return new EmptyResult();
}

You can return PartialView and a parameter to determine which view you wanna render as a Json object : 您可以返回PartialView和一个参数来确定要渲染为Json对象的视图:

public ActionResult LoadView()
{
   if (someCondition == true)
   {
      return Json(new { Url = Url.Action("PartialA",new SomeModel()), viewName = "PartialA"});
   }
   else
   {
      return Json(new { Url = Url.Action("PartialB", new AnotherModel()),
    viewName = "PartialB"});
   }
}

https://stackoverflow.com/a/10266520/6842997 https://stackoverflow.com/a/10266520/6842997

And in the view, after you get result, you can do an ajax post; 在视图中,获得结果后,您可以进行ajax发布;

$.post(result.Url, function (partial) {
       $('#div-a').html(partial);
});

Add PartialViewResults for each Partialview in the controller; 为控制器中的每个Partialview添加PartialViewResults;

public PartialViewResult PartialA(SomeModel model)
{
    return PartialView();
}

public PartialViewResult PartialB(AnotherModel model)
{
    return PartialView();
}

then in your js, do something like this; 然后在您的js中,执行以下操作;

<input type="submit" onclick="load()" value="submit" />

function load() {
   $.ajax(
   {
      type: "POST",
      url: "@Url.Action("LoadView", "Home")",
      data: "", 
      success: function (result) {
          if (result.viewName == "PartialA") {
              $.post(result.Url, function (partial) {
                   $('#div-a').html(partial);
              });
           } else {
              $.post(result.Url, function (partial) {
                   $('#div-b').html(partial);
              });
           }
        }
      });
    }

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

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