How to make a View render Fully or Partially? - c#

I have a HomeController with an action About. I want to achieve this behavior:
Case #1 User open the page /home/about and the view render fully (return View();)
Case #2 User is on the homepage and clicks a link about, I want to change the url using history.pushState and render it partially (return PartialView();)
How can I achieve this behavior?

You can use Request.IsAjaxRequest to detect whether the controller action was invoked using an Ajax request.
As an alternative I'd recommend splitting your views so into smaller pieces, such that your About.aspx view uses the partial view, e.g. by doing Html.RenderPartial( "AboutBox" ). This gives you the power of reuse without having to clutter your actions with if-sentences.

yet another.
Restrict ASP.NET MVC Action's Using The ActionMethodSelectorAttribute
select action method control at ActionMethodSelectorAttribute.

Related

ASP.Net MVC Generate DNS Prefetch Tags

Is there a way to find all the src="" urls when rendering a ASP.net MVC page in the view to then generate DNS prefetch tags on the fly?
https://www.chromium.org/developers/design-documents/dns-prefetching
If I understood correctly I can tell you the following:
Option #1: (Not a pretty solution but would work.)
NOTE: for this try to use simple Javascript and not rely on JQuery or other (since then you still need to "load" the .JS file for that and that is ruining the point of your question.
Process your src/href or some other predefined property tag with some kind of "OwnLogic" to define the "base target",
but in a way that the browser would not be able to initiate the request to obtain that image or other file.
Example:
<img url="" class="DNS_BaseTarget" DNS_BaseTarget="smiley.gif||myCDNPointerInfo" alt="">
Then, with javascript, get a list of all elements that uses the class DNS_BaseTarget and then read the property value and update the "src" tag.
At the same time you can inject by javascript inject all the '<link rel="dns-prefetch" href="https://cdn.yourTargetDomain.com">' that you will use based on the information you just processed.
I did not tested this concept, so "lag" or some sort of delay in the client might be expected (but maybe not noticeble by the user).
Option #2:
The View Result Execution Process (in MVC life cycle) tell us that the method 'Render()' is the last one to be executed.
With this being said, you can create your own custom override logic
Example: intercept view rendering to add HTML/JS on all partial views?
How to intercept view rendering to add HTML/JS on all partial views?
With this concept of trying to "process" the final html before sending it to the user, you could somehow "parse" the file.... try to get all the 'src/href' and then
inject all the '<link rel="dns-prefetch" href="https://cdn.yourTargetDomain.com">' that you will use.

Trouble loading a partial view from a different controller with model data

Quite new to MVC so please bear with me. I'm trying to load a partial view in a modal from a different controller and everytime I try to view it I get just an empty modal. I believe it is because I haven't been able to instantiate my model in the view.
E.g. I have a Controller 'Home' with a method 'Details' that returns a partialview from a different View folder. The native model to the controller is 'model', whereas the model belonging to my other controller is 'model2'.
public ActionResult Details() {
model2.User = user; //this is a global variable
model2.GetDetails();
return PartialView("~/Views/...Details", model2);
}
I'm sure the reason is because i'm missing the model data in the view. I tried adding another #model... to the view but clearly this doesn't work.
Is there a way of doing what I am trying to accomplish? It can even be a relatively dirty solution as this is a stopgap solution for the time being.
Reading back over this post it reads a little convoluted so if any clarification is needed please let me know.
Thanks
I faced this problem once before and i think it's a lot of work to reproduce it to provide an exact solution, but I can offer my 2 cents. The thing with browser Modals is that you need to provide a url when you are opening it. The URL will have to be the Controller/Action url and this is the tricky part which causes the problem. If you can figure that out, you should be able to solve the problem. If you can't, you can do one of the following:
1. Set the HTML content of Modal dialog from your main window's JS code after the Modal is opened.
2. Use one of the 3rd party HTML/CSS modal implementation, and set the HTML content from the JS code. In this case there is no browser modal and everything is on the same page.
To verify if the view is returning correctly from XHR, put the actionRoute URL in the browser address bar and you will see the content getting returned. It will help with troubleshooting.

_Viewstart with controller action

So my current (and I believe this is the default way) to use _ViewStart.cshtml is to do this:
Layout = "~/Views/_SomeSideMenu.cshtml";
Now my sidemenu is a bit complicated so it requires a controller. But I cannot figure out how to call a controller action for the ViewStart and let it return the PartialPage for it.
My current workaround involves using #Html.Action() in the _SomeSideMenu.cshtml file and fill TempData with the things I need.
Normally I would want to put that data in the ViewBag or Model, but these are not available since the Html.Action does not return the _SomeSideMenu.cshtml.
How would one go about calling the controller action to return the PartialPage that is found in the _ViewStart page?
Actually, you cannot do this in the ViewStart file. ViewStart is a file that can store common functionality related to all the views. These pages can also be nested, so if you have multiple controllers, each controller’s views can have their own ViewStart page.
You should add your sidemenu into a Layout page. You could add some conditions to have this output into layout page. For sample, jere, we have an action filter which add a key into ViewBag and we use this key to have a state for a sidemenu (state like open or close and position in left or right).
You also could change your default layout page in your view just doing what the ViewStart page does, chaging the Layout property.
Layout = "~/Views/Shared/CustomLayoutPage.cshtml";

Controller that calls a partial view that should display inside the Layout view

In a standard MVC application we have _Layout.cshtml and an Index.cshtml view. Now imagine if the user receives an email activation after a registration and now he/she clicks on that link.
The link points to /Account/ActivateAccount and after I process the activation, I wanted to redirect the user to the Login.cshtml partial view displaying a message.
I'm aware I can pass the message via the TempData["Message"] but what I don't know is how to redirect to a partial view and have that display inside the _Layout.cshtml instead that by itself.
At least this is what is happening when I call RedirectToAction("Login", "Home")
Note: all "home" based partial views are displaying within a < div id="divmain"> and my ideal solution would be to be able to decide what view should display inside that div, from the controller.
What's the right way to call a Partial View to be displayed from within another Controller ??
I'm not entirely sure I understand the question but I think what you're asking is if you can call a partial view from the _layout? This can be done with #Html.Action() helper. It will call out to a controller and method you specify and insert the result. The method you call would just be a PartialResult that returns the partial view with whatever data you need.
This is in contrast to #Html.Partial() which will render the partial view in place with whatever data you provide without routing back through a controller.
EDIT:
To summarize the comments to this answer, it seems I misunderstood the requirement. A user receives an email and clicks the link to activate their registration. The controller action that handles that request activates the user and then it needs to redirect to something. In this case, there was already a partial view which would serve the purpose but it couldn't be redirected to directly for some reasons. My suggestion was to just create a new View which contained a #Html.Partial() call to basically wrap the partial in a full view page.
The action method which handled the click could then just return that view directly or, if you consider the click of the link to be a "post" since it changes the application model by validating the user, you would create a new controller action and return RedirectToAction() directly. This would be like following the Post, Redirect, Get method and would prevent some issue if the user tried to refresh the "activated" page. It would also give more control over the URL naming.

Need clarification; Post from partial view with no JS

I've been searching for an answer to this question for a while now, I was hoping someone could give me clarity:
If I do a post from a partial View (or a called #Html.Action()) then there is no way save for usage of client-side scripting to have the application preserve state?
phrased differently: I can not post from a partial and just have it "do that part" and have the rest of the controllers remain as they were (provided no unsaved forms ofc)? Again with no java-script or the like.
What happens for me is that when I process the Http.Post and return a partial view, then that is the entirety of what gets written to the output pipeline and everything else in the browser vanishes.
I'm "solving" this right now by passing along a return-URL so I can redirect as an actionresult rather than send a partial view at the conclusion of the Http.Post processing. Is this the best I can do?
Use Ajax to update your partial view
this Question can be helpful for you
Update partial view after submit Ajax.Beginform

Categories