0
votes

Hello I've got an action which gets some data from database and returns a partial view

In the partial view there are ajax.actionLinks which when clicked execute the same ImportShow action but this time with new data; and as you see in the cshtml - then update only the with the new data.

The problem I'm trying to solve is - if a user clicks 'Open in new Window' or 'open in new tab' in the new window you will see loaded only this partial view. And I can't think of a way how to make redirect and reload the whole page only in this cases. (after all the link point to an action method that RETURNS A 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);

            } 

As 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,[email protected]},new AjaxOptions { UpdateTargetId = "importPartUpdate", HttpMethod = "GET", InsertionMode = InsertionMode.Replace })



                  </a> 

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

1 Answers

3
votes

There are a few posts that having this issue before, you can check out this and this. Basically what happens is: when you click the "ajax" link, it is a AJAX call, therefor the partial view was rendered and everything works as expected. However, when you right click to view the page a new tab or new window in the browser, it is NOT a AJAX call, but you're returning a partial view, the new tab or window will still return a partial view. That's why you only see the partial view.

To illustrate what I meant:

here's the code snippet.

    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
            }

        }
    }

Index View:

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

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

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

Partial View:

@model MvcApplication1.Controllers.Person

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