1

Imagine the following architecture with partial views.

Sometimes, when errors occurs in our application for reasons not explained here, the partial view refreshes in full screen. Also, if you copy / paste the url of the partial view, it loads in full screen.

We are trying to find a solution where, when loading a partial view url, it will load correctly the full context and display the partial view in his own context. Windows Azure manages to do this (http://manage.windowsazure.com) and we are willing to mimics this functionality.

We tried some searches on internet with keywords like "refresh partial in correct context", "problem partial view refreshed in full screen", etc... but we do not find yet a way to achieve this correctly.

Is there any framework or solution to do this? What's your ideas on this?

Partial View

CloudAnywhere
  • 669
  • 1
  • 11
  • 27

3 Answers3

1

IF you are using Partial Views its obvious ajax is involved, what you have to do is that do the things of partial view via ajax.

For Example:

If you have form inside partial you need it to Ajax.BeginForm instead of Html.BeginForm and one more thing is that jquery.unobtrusive-ajax file in your master layout to make things work.

Same is the case for other things you must be dealing things via ajax if you are using partial view so that you don't lose context.

I hope it helps.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0

If you want to avoid full refreshes you'll have to use ajax and replace contents inside an id. This should be an easy task if that's what you're planning to do.

You should really only have to pass the partial view url to jQuery.get for example. jQuery.get

I hope that's what you're looking for.

Dbl
  • 5,634
  • 3
  • 41
  • 66
  • Hi Andreas and @ehsan, see comment above adressed to ashwini – CloudAnywhere Apr 09 '14 at 11:23
  • What ashwini described is what i was talking about. Did you maybe declare a layout in your partial view? What he typed down should be doing exactely what it sounds you want to happen. – Dbl Apr 09 '14 at 11:34
  • I will try. But let's say that I have a main page (http://myapp/main) and a partial view http://myapp/home/GetPartialview. If i type http://myapp/home/GetPartialview in the navigator, I don't see any code in the sample provided by @Ashwini that will discover that it must first load the container page (http://myapp/main) which one loaded will call http://myapp/home/GetPartialview – CloudAnywhere Apr 09 '14 at 11:40
  • Ok now this sounds a lot different from what i thought. Your problem is that it does NOT load the container page for the partial view? I thought your problem is that it does reload the container page and the partial view instead of just the partial view? – Dbl Apr 09 '14 at 11:47
  • yes, you have now a good understanding of the problem. It means that somewhere we have to "store" the url of the main container and if we're not coming from this url, it should be loaded.Quite complex to implement to work in several situations, reason why I'm asking if there is an existing, framework, library that would do this. – CloudAnywhere Apr 09 '14 at 11:50
  • This does not sound like something a framework is doing. More like a custom case of accessing different actions depending on calling url. – Dbl Apr 09 '14 at 12:19
0

Partial view meant to be for reusable views that will be used in many places across the website.

I find this very usefull if we load it via ajax.

Following is the example. This would also avoid full screen refresh

_Layout.cshtml (Or any view page which will load the partial page)

<div>
    <h2>This is Partial View content</h2>
    <div id="content"></div>
</div>

<script>
    $(function () {
        $("#content").html("Loading...");
        setTimeout(function () { LoadPartialView(); }, 5000);
    });
    function LoadPartialView() {
        $.ajax({
            type: "GET",
            url: '@Url.Action("GetPartialView", "Home")',
            dataType: "html",
            success: function (data) {
                $("#content").empty();
                $("#content").html(data);
                $("#content").fadeIn('slow');
            },
            error: function (data) {
                $("#content").empty();
            }
        });
    };
</script>

Model

public class TestModel
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Address { get; set; }
}

Controller's Action

    [HttpPost]
    public PartialViewResult GetPartialView()
    {
        TestModel model = new TestModel();

        model.Name="SomeName";
        model.Address="Somewhere";
        model.Age=25;

        return PartialView("_PartialTestPage", model);
    }

UPDATED

For avoiding hardcoded access to the partial view, use overloaded action method which will redirect to the main view. something like this.

    [HttpGet]
    [ActionName("MyOverloadedName")]
    public ActionResult GetPartialView()
    {
        return RedirectToAction("Index", "Home"); //redirect to the main view
    }

You can make two different action method with same name by setting the HttpGet/HttpPost. Look at here for more alternative solution Can you overload controller methods in ASP.NET MVC?.

Community
  • 1
  • 1
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56
  • yes but if you type in the navigator the url of the partial view (eg http://www.mycompany.com/home/GetPartialView), it will load in full screen. What we want to achieve (and Windows Azure has managed to implement that) is to load the main page, then load the partial view in the relevant div ID. – CloudAnywhere Apr 09 '14 at 11:21
  • To further clarify the expected result: If I type in the navigator bar http://testpage.com/controller/bananas or http://testpage.com/controller/Apples where /controller/bananas and /controller/Apples are partialviews, I want the result to be the picture posted in my question. I believe that your answer will not magically load the parent container page that contains the div id="content". – CloudAnywhere Apr 09 '14 at 11:47
  • You can redirect to the main view once someone try to access directly via url. See the example I have added in my answer. – Ashwini Verma Apr 09 '14 at 12:09
  • Thanks Ashwini, it's close to the expected result. Your update will redirect to the main view if coming from a Get. What is missing now is once we have loaded the main view we should be able to load the partial view that was initially called. Maybe the url passed as parameter to the Index function in the controller that would modify the js code and call the partial view in the document.ready fonction? – CloudAnywhere Apr 09 '14 at 13:02