I'm writing an Asp.net MVC application (my first MVC app). I need to pass data to the _Layout view to customize the header and footer on my pages - which user is logged in, if they have any notifications, etc. The _Layout page always needs this information, but the child pages do not.
How should I pass this data to the view? Can I create a LoggedInUser property that the view can access, in the same way there is a Model and ViewBag? LoggedInUser could be populated by the base controller class.
Or is there a better way to implement this?
If you're using MembershipProvider and/or RoleProvider you can do as webdeveloper pointed out to get the identity of the current user User.Identity.Name, if showing it's name is what you want.
Also you could type your _layout to use a specific model, but I don't recommend it. See this question's answer for further details.
Lastly you could populate a ViewBag property on your controllers to have the user information you need.
I wanted to point out that you could do a partial view to achieve this, and avoid _layout typing and populating the ViewBag on each request.
To detect authentication:
User.Identity.IsAuthenticated
Then you can use sections ASP.NET MVC 3: Layouts and Sections with Razor or/and #Html.RenderAction (Html.RenderAction and Html.Action)
Related
I have a small confusion in Asp.Net MVC
How rendering works in Asp.net MVC? We invoke View function - > Which will find the view and ask ViewEngine to parse it. Because of ViewEngine final outcome is HTML.
1)Whatever ViewData we create its available inside View. My understanding is ViewData and View function both are part of controller base class which makes ViewData available inside View function. Is it correct?
2)Finally Whats the point with WebViewPage class. ViewData keyword we use inside View(.cshtml) page is coming from the WebViewPage class. What role WebViewPage plays here.
I will really appreciate If you can point me with some good resource to understand the same
1) ViewData is merely a dictionary of objects that you can fill in the Controller and retrieve within the view. Since it is a dictionary of objects you need to cast the data back into the type it was to make full use of it.
2) WebViewPage is the base type of a razor page. It is the defined class which razor pages are compiled into at runtime. The web.config inside the views folder specifies the pageBaseType of the razor pages specifically to WebViewPage. These are two good resources regarding why its use and how you can extend it. Link1 and Link2.
Go peek inside he source code that renders the views
visit msdn
I use MVC 3 with spark view engine. I have a data that should be accessed in every pages, the data base-on user login information. so I collect the data when user loged in and I put it on a session. I use a control to handle the data in site master ( master page ). and data in session will be binded to that control (eg. Dropdownlist ).
is it the best practise to handle data in master page on asp.net MVC ?
I try to create a base class for controller, buat I can't hold the login credential (HttpContext.User.Identity).
how can I access session data from View ?
I try to use #Session["mydata"] ( !{Session["mydata"] in spark view engine} but it didn't works :(
any helps ?
There are two general ways to handle this.
One is to use Html.RenderAction from your layout (master page). In that way you are packaging all of your data ahead of time into a model the master page will use.
The second way is to have your controllers inherit from another controller. When the base controller is instantiated, it is responsible for populating ViewData with the required data which is then displayed on the page. This requires all of your controllers to inherit from your base controller.
See listing 4/5 at:
http://www.asp.net/mvc/tutorials/passing-data-to-view-master-pages-cs
You could also do this via an action filter (and other ways too!!) but I think the above two ways are a bit cleaner
You should be able to get to the credential of the logged on user by calling System.Threading.Thread.CurrentPrincipal.Identity.Name in your controller base class.
That should allow you to populate the ViewData and then access the data from your view.
Alternatively if you still want to use the session, you should be able to access the session via - HttpContext.Current.Session["mydata"]
you can use TempData["name"] for sharing data on different actions in a controller. Remember once value is retrieved, it will delete the data but you can use .Peek and .Keep method functionality to store its value
I have a user control which shows list of latest announcements. This user control would be used in almost 90% of my pages. Now my concern is how to pass data to this user control for latest announcements.
My first approach is to make a base controller and in Initialise method I pass data to user control via ViewBag/ViewData. All my other controllers derive from this base controller. This looks nice but my concern is that it may become an overkill for some simple solution existing already out there. Also I would need to make sure that no controller ever fiddles with my Viewdata/Viewbag data meant for my usercontrol.
Please let me know if this is correct way of proceeding ahead or there exists some better solution.
Thanks
Assuming you have a "user control" (you should try to refer to them as partial view's in MVC) that looks like this:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Announcement>>" %>
This means your partial view expects a list of Announcement objects.
Now, the question is - where are you rendering this partial view?
You could be doing it from a master page, you could be doing it from a view, or you could be doing it from another partial view.
Either way, the code to render the partial needs to look like this:
<% Html.RenderPartial("LatestAnnouncements", announcements) %>
But - where do you get the announcements from.
Assuming you have a Repository/DAL/helper method to get the latest announcements - i think you should have the ViewModel's you require inheriting from a base ViewModel:
public class AnnouncementViewModelBase
{
protected IEnumerable<Announcement> GetAnnouncements()
{
// call DAL
}
}
Then any master/view/partial that needs to render the latest announcements partial should be bound to a ViewModel which inherits from that base view model.
In the cases where the master/view/partial is not strongly-typed (e.g dynamic view), you can stick it in the ViewData. But if you have organized your view's correctly this shouldn't be required.
Is this the kind of thing you're after? How to pass data from view to UserControl in ASP.NET MVC?
You should use RenderAction in this kind of scenario, so that you do not have bother to pass the required data in each action method of your controllers.
I think the best way would be to use #Html.Action. This would allow me to call my actions dedicated to my usercontrols data and I can call it from anywhere.
I'm looking for a best practice for embedding a form on multiple pages as a partial view.
I have a contact form I'm looking to embed on multiple pages on a site. Usually, the form would be on a contact page and the contact model could be the model for the view and use data annotations for validation. However, the view is already strongly typed.
How can I create a reusable form in a partial view and embed it on the page? I'm using N2 on the site, so the pages have to already have a strongly-typed model, but I would be open to extending those objects.
Personally, I recommend using for Html.RenderAction() for cross-cutting concerns such as these.
The handler for your contact form is going to need to exist independently of the page your are currently viewing so you are left with 3 options:
Manually add it to the response of
the current action
Manually add it to the response of
the current controller by way of a
base controller that modifies the
ViewState or ViewModel
Call the RenderAction()
HtmlHelper inside of the current view
Of these 3 options, while the third is technically more costly than 1 and 2 (because it initiates a brand new request), it is also the most maintanaible solution. By calling RenderAction() you have the advantage of being able to completely isolate your contact form from the rest of the view and thus you won't have to worry about hacking it into the current controller responses.
Use RenderPartial if data model for partial view is already in main view's model, in other case use RenderAction (then the action of the partial view will create its view model itself).
How would one go about performing controller actions from withing an ASP.net MVC user control?
My scenario is that I have a userID and I want to transform it into a name from the database. To do this I've annotated my model with a display type and put a User template in the shared display templates but I'm not sure where I should write the code which does the lookup to convert from userID to user name.
I think that code ought to go into your models and you should be calling it in your controller and passing it to your user-control in a viewdata. This is if I understood your question.
I would just have the model expose the name and not the userID. This way your view (user control) is only displaying the name and not trying to do a DB lookup. Your "User Control" model would be responsible for how it gets the name, i.e. the DB from your question.
In short, you don't do that.
You should be passing the necessary data to the MVC user control from the View, which in turn should be getting it's information from the controller.
The view (or user control) should not have any knowledge of the controller. You may want to use RenderAction instead of a user control if you feel that the view shouldn't be responsible for passing the necessary information into the user control.