【问题标题】:handle 'open in a new window' when clicked on ajax.ActionLink which point to action that returns partial view单击 ajax.ActionLink 时处理“在新窗口中打开”,它指向返回部分视图的操作
【发布时间】:2014-07-18 22:35:02
【问题描述】:

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

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

我要解决的问题是 - 如果用户在新窗口中单击“在新窗口中打开”或“在新选项卡中打开”,您将看到仅加载此部分视图。而且我想不出只有在这种情况下才能进行重定向和重新加载整个页面的方法。 (毕竟链接指向一个返回部分视图的操作方法)。

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);

            } 

作为 模型 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>

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    之前有几个帖子有这个问题,你可以查看thisthis。基本上发生的情况是:当您单击“ajax”链接时,它是一个 AJAX 调用,因此呈现了部分视图并且一切都按预期工作。但是,当您在浏览器中右键单击以在新选项卡或新窗口中查看页面时,这不是 AJAX 调用,但您正在返回部分视图,新选项卡或窗口仍将返回部分视图。这就是为什么您只能看到部分视图。

    为了说明我的意思:

    这是代码 sn-p。

        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>
    

    【讨论】:

    • 我完全理解!!!我该如何克服这个问题 - 这个 importshow 部分视图是我的索引视图的一部分
    猜你喜欢
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    相关资源
    最近更新 更多