当单击ajax.ActionLink时,处理'在新窗口中打开',指向返回部分视图的操作

时间:2014-05-29 12:12:16

标签: asp.net-mvc

您好我有一个从数据库获取一些数据并返回部分视图的操作

在局部视图中有ajax.actionLinks,单击时执行相同的ImportShow操作,但这次使用新数据;正如您在cshtml中看到的那样 - 然后仅使用新数据更新。

我试图解决的问题是 - 如果用户点击“在新窗口中打开”'或者在新标签页中打开'在新窗口中,您将看到仅加载此部分视图。我无法想出如何在这种情况下重定向和重新加载整个页面的方法。 (在所有链接指向返回PARTIAL VIEW的操作方法之后)。

public virtual ActionResult ImportShow(String id, String menuID, string articlegroupID, int? counter)
        {

                 GroupMenu p_GroupMenu = new GroupMenu();

                p_GroupMenu.MenuHistory = p_GetMenuHistory.ToList();
                p_GroupMenu.MenuLeft = p_GetMenuLeft.ToList();
                return PartialView("ImportShow", p_GroupMenu);

            } 

作为 model MvcBeaWeb.GroupMenu

<div class="importPartUpdate">
    <ul id="products">
    @{
        if (Model != null)
        {
           foreach (MvcBeaDAL.WebServiceBeaMenu item in Model.MenuLeft)
           {

              <li id="@item.ID">
                <div class="imageTilesShow">
                  <a  title= @item.SpecialWord>
                    <img src="@item.ImageFile"  alt='@item.SpecialWord)' id="ImageProducts"  class="imageTilesShow"> 

                    @Ajax.ActionLink(@item.SpecialWord, "ImportShow", new { id = Model.LanguageName,menuID=@item.ID},new AjaxOptions { UpdateTargetId = "importPartUpdate", HttpMethod = "GET", InsertionMode = InsertionMode.Replace })



                  </a> 

                </div> 
              </li> 
           }
        } 
      }                                           
    </ul>
 </div>

1 个答案:

答案 0 :(得分:3)

之前有一些帖子存在此问题,您可以查看thisthis。基本上会发生什么:当你点击&#34; ajax&#34;链接,它是一个AJAX调用,因此部分视图被渲染,一切都按预期工作。但是,当您右键单击以在浏览器中查看新选项卡或新窗口时,它不是AJAX调用,但是您返回部分视图,新选项卡或窗口仍将返回部分视图。这就是为什么你只看到局部视图的原因。

说明我的意思:

这里是代码段。

    public class HomeController : Controller
    {
        List<Person> people = new List<Person>()
        {
            new Person { Name = "Larry", Age = 10},
            new Person { Name = "Jessie", Age = 11},
            new Person { Name = "Ben", Age = 12},
            new Person { Name = "Victor", Age = 13},
            new Person { Name = "Tom", Age = 14},
            new Person { Name = "Suresh", Age = 15},
            new Person { Name = "Jim", Age = 16},
        };

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetPerson()
        {
            Random r = new Random();
            int i = r.Next(0, people.Count);

            if (Request.IsAjaxRequest())
            {
                return PartialView(people[i]); //return partial if it's a ajax call
            }
            else
            {
                return View(people[i]); // return view if it's NOT a ajax call
            }

        }
    }

索引视图:

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@Ajax.ActionLink("replace", "GetPerson", new AjaxOptions { UpdateTargetId = "replaceMe", HttpMethod = "Get", InsertionMode = InsertionMode.Replace})

<div id = "replaceMe"></div>

部分视图:

@model MvcApplication1.Controllers.Person

<div>
    Name : @Model.Name <br />
    Age : @Model.Age
</div>