Here are some real-time asp.net mvc examples that will help you to understand asp.net implementation better; you may find some of them are very useful while developing any asp.net application.
This is an example Loading doropdownlist in MVC2 and MVC4
Loading dropdown list from database values can be very common requirement in any application development, here you learn how you can fetch database values in your mvc controller code to set in list item property.
Here i am assuming you know how to fetch data from sql database, let's call that service method as dataService.GetCountries();
as shown in example below.
Also note, that we have used ViewBag.CountryDropdown
property
to hold the database value, you also can create a property of List<SelectListItem>
type in your model
[HttpGet] public ActionResult ManageProfile() { ViewBag.Countries = dataService.GetCountries(); ViewBag.Message = string.Format("There are {0} contries", dataService.GetCountries().Count); List countryList = (from p in dataService.GetCountries().ToList() select new SelectListItem() { Value = p.CountryId.ToString(), Text = p.CountryName }).ToList(); ViewBag.CountryDropdown = countryList; return View(); }
Now we see how to load dropdown list values in razor view. you need to cast the values into List<SelectListItem>
type (if not using strongly type property).
List<SelectListItem> countryList=ViewBag.CountryDropdown as List<SelectListItem>; @Html.DropDownList("ddlCountry", countryList) // OR @Html.DropDownListFor(m=>CountryList, countryList)
This is can be very useful when you want to populate dropdownlist values based on select value of another dropdownlist. take a look at implementation example.
Create custom AuthorizeAttribute example in mvc4 razor, check authorization before user perform any action.
Think of situation where you need to check user authentication before showing the page details, now one way you can write additional code on every action, and check authentication first, if not authenticated then redirect user to different view page.
But, another smart way can be writing an attribute class like example below and apply that class on every action wherever you want to check authentication.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)] public class AuthorizeAdminAuthorizeAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { var isAuthorized = base.AuthorizeCore(httpContext); if (!isAuthorized) { isAuthorized = false; } if (httpContext.User.Identity.Name == null) isAuthorized = false; else isAuthorized = true; return isAuthorized; } protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { if (!filterContext.HttpContext.User.Identity.IsAuthenticated) { base.HandleUnauthorizedRequest(filterContext); filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "AdminAccount", action = "Index" })); } else { filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "JewelAdmin", action = "Index" })); } } }
Now we see how to apply that authentication attribute class on any action, we can also directly apply on controller level too.
Now whenever you make changes in your authentication logic, that will be automatically applied all the actions or controller wherever you have set that authorization attribute.
[AuthorizeAdminAuthorize] public ActionResult ShowMyProducts() { return View(); }