ViewModels or ViewBag?

Basically, Should everything be done through the viewmodel or is it Ok to also incorporate viewbag? Everything should be done inside a view model. That’s what a view model is. A class that you specifically define to meet the requirements of your view. Don’t mix ViewBags with ViewModels. It is no longer clear for the … Read more

MVC 4 Beta side by side installation error

After installing MVC4 beta today, a few of my MVC 3 projects would not compile. (ModelClientValidationRule conflict) The fix was: Edit: ProjectName.csproj Change <Reference Include=”System.Web.WebPages”/> To <Reference Include=”System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL “/>

How to develop an ASP.NET Web API to accept a complex object as parameter?

By default complex types are read from body, that’s why you are getting null. Change your action signature to public IEnumerable<Users> Get([FromUri]MyApiParameters parameters) if you want the model binder to pull the model from the querystring. You can read more about how Web API does parameter binding in the excellent article by Mike Stall from … Read more

How am I supposed to use ReturnUrl = ViewBag.ReturnUrl in MVC 4

When using forms authentication and the user is not authenticated or authorized the ASP.NET security pipeline will redirect to the login page and pass as a parameter in the query string the returnUrl equal to the page that redirected to the login page. The login action grabs the value of this parameter and puts it … Read more

Automatic Migrations for ASP.NET SimpleMembershipProvider

update-database -verbose doesn’t work because your model has been changed after your data table already existed. First, make sure there are no changes to the UserProfile class. Then, run: Add-Migration InitialMigrations -IgnoreChanges This should generate a blank “InitialMigration” file. Now, add any desired changes to the UserProfile class. Once changes are added, run the update … Read more

ASP.NET MVC 4 Editor Template for basic types

Editor templates work by convention. The name of the template must match the name of the type. So for example if SomeValue is an int type you could write a custom editor template at ~/Views/Shared/EditorTemplates/Int32.cshtml which will be used. In this case all integer types will use this custom template when you write @Html.EditorFor(model => … Read more