Need a suggestion on error handling in partial views - c#

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

Related

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

Display form in pop up

i have two cshtml page one which has the link the makes the popup appear, and another with just the form data and would like to know how i would be able to display the form on the popup, as i am using MVC, to create the form, the plage which has link on is in the ~/views/client/index.cshtml while thr form is ~/views/fb/CreateOrEidt.cshtml the colller is called "FB" and the methos to call is edit, it take the parameter Id
I have tried #hmtl.renderpartial, #html.renderaction, #hmtl.partial, #html.action,
I have also tied these method with a { after the # and the end, doesnt give me a error bust still doesn't display information
The error which i get is razor canot convert type object to void
It's a little unclear what you're trying to achieve but from the error you've mentioned when calling Html.RenderAction you need to call using something like:
#{
Html.RenderAction("ACTIONNAME");
}
Calling the other methods against an action view won't work.
If you're trying to display a view inside a popup though you want to be careful that you don't end up including your layout page etc as this is potentially not the look you're going for..!
The pattern I generally implement for things like this is to simply partial out the form bit that I need to reuse and call renderpartial to display this where I need it. This renders just the html I'm after then and not the whole view (+ layout(s)).

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

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.

Determine if View is being rendered as Partial

Is there a way to determine if a view is rendering as a partial?
I'm hoping to extend the reuse of a partial that I'm writing by catching this...and if necessary assigning the appropriate layout to the View.
At the moment I'm just rendering it in a div, but I could also see us using it as a modal and possible it's own page.
(the modal shouldn't require any change so no worries there)
EDIT:
To clear up what I'm asking.
I'm wondering if there is anyway to determine the difference between a view being rendered by...
/path/to/controller
and
Html.Partial("/path/to/view.cshtml")
Why not #if (Layout==null)?
Still I would recommend another view for the "own" page and set the layout there.
In your view (assuming Razor syntax):
#if(typeof(this) == Controller.PartialView)) //code
or
#if(this is Controller.PartialView) //code
Based on #Pheonixblade9 's response and the lack of other answers it doesn't appear like this is possible at the moment. I ended up just binding the Model of the View as bool and pass this value in when rendering the view/partial.

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