Getting the current URL within the View layer in ASP.net MVC - c#

I've read some previous questions here and on other sites, but being new to ASP.net and MVC I'm having a bit of trouble understanding the information presented.
I want/need/was told to get the current URL of the page I'm on through the View layer, and use that information to apply an id to a li tag allowing for specific css. We've moved our left navigation bar from being embedded in every single page (done by a previous co-op) to putting the list in a partial view that I'm going to call on all of the required pages. Styling requirements for the site have a specific highlight on the left navigation a tag of the page the user is currently on.
Some of the examples I've read including using:
<%= Request.Url.PathAndQuery %>
Request.Url.ToString() or Request.Url.AbsoluteUri
var request = HttpContext.Current.Request
but I know not all of them can be used in the View layer. What would be the best approach? Are there any tutorials that I haven't been able to find yet that anyone could recommend?

It probably isn't the best idea, in my opinion, to use the URL for this.
Instead, a quick and easy way to achieve this is to use ViewContext.RouteData that will contain values for both the controller and action of the current request. It can be accessed from the view layer easily.
ViewContext.RouteData.Values["Controller"].ToString()
ViewContext.RouteData.Values["Action"].ToString()
So in your view you could do something like
<ul class="nav">
<li class="#(ViewContext.RouteData.Values["Controller"].ToString() == "ControllerName" ? "active" : "")">Foo</li>
</ul>
You could push it further to make it prettier, but you get the basic idea.

I was looking for a solution on this for asp.net 3.0 and found the following to give direct access to the URL path (what page you're currently loading/showing):
ViewContext.HttpContext.Request.Path
I'm using this in an if statement to decide dynamically between different View Imports, works very well.

Related

How do i get data from one "View" into another "View" using MVC

That is the basic question. I have looked through a bunch of posts on SO and im not finding quite what im looking for.
Very simple, if I am on Index.aspx with data vars populated in my javascript, how do I call an action in my controller and load a new page where i can populate the vars i sent from the index page?
I currently have solutions that get me most of the way there but not all the way. I am new to MVC so i am probably over complicating something that is simple and then looking in the wrong places for an answer ... Please Help
The only way i have managed to get this to work so far is by doing an ajax post request sending the data variables i need from Page 1 , Setting the Session Variable then in the ajax.success section on page 1 I redirect to page 2 using window.location.href = "~MyUrl~"
This works .... but there has to be a better way of doing it ...

MVC Razor & AJAX paging

Is there a way in which I can construct a MVC page such that, if required, I can pull the contents of it without the entire HTML frame. I.e. I want to be able to, if required, pull just the contents (for AJAX Paging) without the refreshing the entire page, but I want that to be possible too
#{
ViewBag.Title = "ViewDevice";
}
<h2>ViewDevice</h2>
You can use partial view to implement reusable part of a view and render it in any view you want like this #Html.Partial("_ViewDevice").
For more information on how to create a partial view see here and here.
Here is a great tutorial thats easy and quick:
http://www.joe-stevens.com/2011/05/30/asp-net-mvc-simple-server-side-ajax-paging-using-jquery/
check it out: https://github.com/kibiluzbad/Ifa
sample online demo: ifademo.apphb.com/
hope it helps

Can I populate a ContentPlaceHolder in a master page from within a Razor Partial View?

I'm using the typical built in view engine in mvc3 (is there a proper name for it?) for views and master pages and it's including a Razor partial view on the .aspx page. In the masterpage, there is a ContentPlaceHolder with an ID of "ScriptContent".
I want to be able to fill that ContentPlaceHolder from within the Razor partial view but I don't think this is possible. Does anyone know if it is possible and how I would go about doing that?
I already tried rendering it in the partial like so, but that didn't work.
#section ScriptContent {
... content ...
}
It would be very difficult, so much so that I'd recommend finding another way :(. I wish it was easier, but these are the complexities of integrating a new view engine into an existing legacy system.
To give you a head start if you really want to try it: You'd probably need to create a custom base class inheriting from WebViewPage for your Razor content pages, override some of the methods (honestly I'm not too familiar with that aspect so you'd need to debug to follow the pipeline) so that instead of treating the Layout property as the path to a Layout page, you treat it as a Master page. Then you'd need to instantiate the master page and somehow convert the Sections (which were transformed into calls to DefineSection by the Razor parser, and should be stored in a Dictionary somewhere on the base class) in to Content controls and stuff them in the Master Page.
If I haven't boggled your mind by this point, you may just be able to pull this off, but to be honest, I'd avoid it.
P.S. We refer to the older view engine as "ASPX", based on its file extension ;).

Need a suggestion on error handling in partial views

I can't seem to find an answer to this. Basically, inside of a View, I got
#{
Html.RenderAction("PartialViewAction", "SomeController");
}
Let's say, this partial action throws an exception: DB connection is out, or whatever. How would I display the error to the user?
All articles/blogs that I see talk about error handling in (non-partial) Views and partials rendered on AJAX calls. But this case is neither.
I think each view should handle their exceptions, so your logic should be in the view, but if we keep in mind MVC rules, exception handling should be on controllers, because they are in charge of communicating with the model. So the question is how make them show in the final view. I don't know if this could be helpful, but I will try a workaround.
The idea is subscribe your exceptions during a request in a list for example, an elegant solution could be having this list in your Viewbag, and then in your global layout write them in the view for example
<ul name="hiddenExceptions" class="hidden">
<li>exception 1 message wrote in the layout</li>
<li>exception 2</li>
</ul>
You can have an script later that iterates through this list an take appropriate actions, show them in a box, show at the top of the page, etc.
Hope it helps

ASP.NET MVC how to create a model which pulls in data from 2 tables, to display in 1 view

I think this may be a very simple question, but I am only just starting out with .net and c# at all, and only really just finally getting my head around OO stuff.
I have built the NerdDinner application, and I am building on top of it currently with my own project.
What I need to do, in the context of nerd dinner, is display the details of a dinner, but also show all the assoicated RSVP's on the same page.
The url could be the same as normal
dinners/details/2
but the 2 would be used to bring back all rsvp's related to that, and display them in a list on the same page.
I have spend some time trying to do this in the dinnerRepository.cs file, but I'm getting a bit stuck, and not sure the best way to do this.
I would then like to be able to add more rsvp's from that same page (I understand that dosent work in this example, but you seen that I am trying to Add more of rows to tableB, related to a single row in tableA)
Please only answer the first question if you feel the second part should be asked elsewhere.
Thanks so much for any help.
You do not need to change the dinnerRepository.cs file at all. The dinner model already has a RSVP collection. You will just need to modify the Details view as such:
<p>
<% foreach(NerdDinner.Models.RSVP rsvp in this.Model.RSVPs)
{ %>
<%= Html.Encode(rsvp.AttendeeName) %>
<br />
<%} %>
</p>
Your done.
IIRC, the details view is passed an instance of Dinner. Simply render out the RSVPs for that dinner on the details page.
I like using RenderAction for this kind of thing. RenderAction allows you to target a specific subcontroller method to render a subview in the page. Since it is a modular technique, it can be used in more than one page without changes.
RenderAction is part of the Futures assembly: Microsoft.Web.Mvc.

Categories

Resources