Authorize attribute in ASP.NET MVC

Real power comes with understanding and implementation membership provider together with role provider. You can assign users into roles and according to that restriction you can apply different access roles for different user to controller actions or controller itself. [Authorize(Users = “Betty, Johnny”)] public ActionResult SpecificUserOnly() { return View(); } or you can restrict according … Read more

MVC3 Non-Sequential Indices and DefaultModelBinder

I have this working, you have to remember to add a common indexing hidden input as explained in your referenced article: The hidden input with name = Items.Index is the key part <input type=”hidden” name=”Items.Index” value=”0″ /> <input type=”text” name=”Items[0].Name” value=”someValue1″ /> <input type=”hidden” name=”Items.Index” value=”1″ /> <input type=”text” name=”Items[1].Name” value=”someValue2″ /> <input type=”hidden” name=”Items.Index” … Read more

CheckboxList in MVC3.0

You could have a view model: public class MyViewModel { public int Id { get; set; } public bool IsChecked { get; set; } } A controller: public class HomeController : Controller { public ActionResult Index() { var model = new[] { new MyViewModel { Id = 1, IsChecked = false }, new MyViewModel { … Read more

MVC, EF – DataContext singleton instance Per-Web-Request in Unity

Yes do not share context and use one context per request. You can also check linked questions in that post to see all problems which a shared context caused. Now about Unity. Idea of PerCallContextLifetimeManager works but I think provided implementation will not work for more than one object. You should use PerHttpRequestLifetimeManager directly: public … Read more

Razor HtmlHelper Extensions (or other namespaces for views) Not Found

Since the Beta, Razor uses a different config section for globally defining namespace imports. In your Views\Web.config file you should add the following: <configSections> <sectionGroup name=”system.web.webPages.razor” type=”System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″> <section name=”host” type=”System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″ requirePermission=”false” /> <section name=”pages” type=”System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″ requirePermission=”false” /> </sectionGroup> </configSections> <system.web.webPages.razor> <host factoryType=”System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, … Read more

How Should I Declare Foreign Key Relationships Using Code First Entity Framework (4.1) in MVC3?

If you have an Order class, adding a property that references another class in your model, for instance Customer should be enough to let EF know there’s a relationship in there: public class Order { public int ID { get; set; } // Some other properties // Foreign key to customer public virtual Customer Customer … Read more

What is the @Html.DisplayFor syntax for?

Html.DisplayFor() will render the DisplayTemplate that matches the property’s type. If it can’t find any, I suppose it invokes .ToString(). If you don’t know about display templates, they’re partial views that can be put in a DisplayTemplates folder inside the view folder associated to a controller. Example: If you create a view named String.cshtml inside … Read more