.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

ModelBinders obsolete in MVC2?

Posted By:      Posted Date: August 24, 2010    Points: 0   Category :ASP.Net


I have a shopping cart that uses MVC 1 (guide from the book "Pro ASP.NET MVC Framework" by Steve Sanderson). Now that MVC 2 and VS2010 are released, the shopping cart no longer works.

Is there any changes about the ModelBinders.Binders.Add(), is it obsolete in MVC2? The code binder below wasn't hit at all in Application_Start(). Sad


    Sub Application_Start()

ModelBinders.Binders.Add(GetType(ShoppingCart.Model.Cart), New CartModelBinder)
End Sub


Imports System.Web.Mvc

Public Class CartModelBinder
Implements System.Web.Mvc.IModelBinder

Private Const cartSessionKey As String = "_cart"

Public Function BindModel(ByVal controllerContext As ControllerContext, ByVal bindingContext As ModelBindingContext) As Object Implements IModelBinder.BindModel
If (Not (bindingContext.Model) Is Nothing) Then
Throw New InvalidOperationException("Cannot update instances")
End If
Dim cart As ShoppingCart.Model.Cart = CType(controller

View Complete Post

More Related Resource Links

MVC2 issue - Stuck. Trying to create a cms with nested partials


I am currently trying to display multiple items on a single page. I am not sure as to how to tackle this. What I have is a database table that has all the page data in. I have this run as a PagesController and this works fine. However what I would like to do is, if say the Products page is selected via the menu, I want to pull back not only the Products page html but also get all the products, and even be able to pull them through via category. I was trying to use a partial view, but I can't get it to work. The same would be for other pages, having partial views to display the other content, ie if Gallery is selected, the page html for Gallery shows and it then can push to a partial view that pulls all the gallery images from the gallery db table and so on. 

Maybe I am tackling this the wrong way. I have the pages stored in the db to allow for a CMS system I have setup. Maybe I need to use models for all the other items(Products, Gallery, Videos and such) and use the partial for the page html?

Any help would be greatly appreciated as I am confused.

areas mvc2 iis 6.0


hi all,

i have another problem. I am trying to deploy my MVC2 website on iis 6.0. I have added the following to my global.asax:


routes.MapRoute("Default","{controller}.mvc/{action}/{id}", new {controller = "Home", action = "Index", id = UrlParameter.Optional } );

I added the ".mvc" extention because i read that iis 6 needs this. And it works. BUT!! I also use areas, and this is something iis 6 does not understand.

I have the following:
No Areapath: Home.mvc/Index (works fine, no area used)
Areapath: Projects/Overview/Index (does not work!!)

I don't now how to solve this, i hope somebody can help me.




Tip #107 Did you know . How to improve debugging performance for MVC2 application in Visual Studio 2

VS2010 Ultimate included IntelliTrace functionality by default.  IntelliTrace captures events (e.g. exceptions), and can affect performance of debugging. The typical performance hit is about 5% in the default mode (collection can be increased to...(read more)

How to switch between HTTP and HTTPS in ASP.NET MVC2


ASP.NET MVC2 has the new RequireHttpsAttribute that you can use to decorate any action to force any non-secure request to come through HTTPS (SSL).  It can be used as simply as this:

        public ActionResult LogOn()

Now any request to the LogOn action that is not over HTTPS will automatically be redirected back to itself over HTTPS.  This is very useful and easy to implement.

Unfortunately though, once the scheme is changed to HTTPS, all following requests will also be under HTTPS, which is not necessarily what you want.  In most cases you do not need all requests to your site to be secure but only certain ones such as the logon page or pages that accept credit card information. 

To handle this you can override the Controller.OnAuthorization method.  From within this method, you can check to see if the RequireHttps attribute is set on the Controller Action by searching the Attributes collection of the ActionDescriptor object. If the RequireHttpsAttribute is not set AND the request is under SSL, then return a redirect result to the HTTP (no

Multiple forms on the same page MVC2: how to handle (including validation from model)



It should be simple; although I'm having a hard time to figure out the best way to do it. I've MVC2 pages with multiple forms on the same page having different functionalities.

What's the best way to handle this; including the MVC2 way of validation from the model?


MVC2 Music Store - How to get store controller - store.aspx working {genre names displayed}



<p>Select from <%: Model.NumberOfGenres %> genres:</p><ul> <% foreach (string genreName in Model.Genres) { %>

<li> <%: genreName %> </li>

<% } %>


MOST PEOPLE WILL BE USING VISUAL STUDIO 2008 sp1, to get this to work change the <%:, ONLY WITH SEMI COLONS, TO <%=

Asp.net MVC2 Linq to Sql



I am a little new to this concept so bear with me.

1) I need to query atleast 35 tables using linq to sql(SQL server database) . I am not able to add all the database entities in Linq to sql classes?

2)(View Model issue) When I use List<Reportcollection> and also List<Receipt> to convert the result then its giving an error that "Cannot convert list<anonymous> to list<string>"


Please suggest me both the issues. Thanks in Advance


ASP.NET MVC2 -- Linq to sql -- Crystal Reports



I need to generate almost 73 reports using Crystal reports using SQL SERVER. I might get more data from XML so opted ASP.NET MVC2.

I have 2 questions:

1) I have a lot of tables to generate Data Context around 35 ( I was thinking to write more stored procedures). Any help with that?

2) Is it possible to create one Report viewer/Crystal report  to display these reports ( I just need to  Read data by querying on input No CRUD operations)

If somebody can explain me the best process to use. Thanks in Advance. 

Help! "You must rerun snapshot because current snapshot files are obsolete" every 90 minutes!

I'm having difficulty with SQL Server replication getting machines in the field to replicate correctly.   Here are the specifics of my installation:   100 remote tablet PCs using web sync to update. 1/2 running Vista, 1/2 XP (all latest updates applied) SQL Server Express 2005SP2 w/ cumulative update 6.   Server: Win2003 (all latest updates) SQL Server 2005SP2 CU6   There is 1 database. For each tablet I create a separate publication (PUB001, PUB002, PUB003, etc.) with filtered articles containing data for the tablet. Each publication has a single subscription -- the tablet to which the subscription is assigned. Subscriptions are set to expire after 14 days. Snapshot agent was run when subscription was created, and is set to run automatically every 14 days.     This is a "test" system which is scheduled to roll out into production in 2 weeks.   For the past month I have been running 6 tablets A-OK with no problems (had 6 separate publications/subscriptions on the server/tablets).   I use a stored procedure to create the publications and subscriptions. This stored procedure hasn't changed in months.   Yesterday, we added another 6 tablets w/ separate publications on the server, using the same aforementioned stored procedure.     The tablet application is set to auto-sync over the internet every 15 minutes.   Starting yesterday afternoon, at random, tablets would st

Why does MVC2 reset my Context.User but MVC1 doesn't?

The question comes from a previous thread, but I would like to ask a more specific question from anyone in the know.Why does MVC2 reset my Context.User, discarding changes made in Application_AuthenticateRequest but MVC1 does not?Repro is pretty simple:Start an MVC1 and MVC2 new project. In both of them add the following code in global.asax: protected void Application_AuthenticateRequest(Object sender, EventArgs e) { if (Context.Request.IsAuthenticated) { FormsIdentity ident = (FormsIdentity)Context.User.Identity; Context.User = new System.Security.Principal.GenericPrincipal(ident, new[] { "FooAdmin" }); bool result = User.IsInRole("FooAdmin"); } } // break hereNow in both HomeController.cs files change the Index Action to: public ActionResult Index() { bool result = User.IsInRole("FooAdmin"); ViewData["Message"] = ""; // break here return View(); }Deploy to IIS, attach debugger on the "// break here" lines and then login to each site in turn.You should find that under MVC1 the result variable at both breakpoints is true, but in MVC2 it is false in the Index action.Anybody have any idea why? Is this a bug or by design? Any way around it?

error in MVC2 on empty string form fields which are required: "This property cannot be set to a null

In model I have:[DisplayName("ISBN")] [Required(ErrorMessage = "ISBN is required.")] [StringLength(50, ErrorMessage = "ISBN book must have max {1} chars.")] [DisplayFormat(ConvertEmptyStringToNull = false)] public string isbn { get; set; }But when I submit form with empty field ISBN I have an error - ConstraintException: This property cannot be set to a null value.I have find that other people have the same problem and they say that debbugger in VS2010 works wrong - yes ?http://p2p.wrox.com/book-professional-asp-net-mvc-2/79788-constraintexception-unhandled-user-code.html#post259245http://efreedom.com/Question/1-3129080/Server-Side-Validation-REQUIRED-String-Property-MVC2-Entity-Framework-Workhttp://mvcmusicstore.codeplex.com/workitem/6604

WSE is Obsolete - Use Only If Necessary

To my fellow Web Service Developers:I have repeatedly encountered developers who learn about WSE (Web Service Enhancements) through Google or some other search engine. In many cases, they were not searching for WSE, but instead were searching for "web service security" or "web service outside of IIS". Because the search turned up a WSE article, some of these developers are starting off with WSE when they don't need to.I want to make clear what I so frequently have to repeat: WSE is obsolete. It has been replaced by WCF (Windows Communication Framework). WSE is not supported by Visual Studio 2008 or above. This should give you an idea about how Microsoft thinks about WSE. If your project is at all important, and if you expect it to last more than a few months, then you will not be doing yourself any favors by depending on this obsolete software.WCF is supported by .NET 3.0 and above, though your best option would be .NET 3.5 SP1. Some organizations will not upgrade to .NET 3.5 because they are concerned about the impact on their existing .NET 2.0 applications. There is little need for concern: .NET 3.5 SP1 consists of .NET 2.0 SP2 and some additional assemblies that are not used by your existing .NET 2.0 applications. It will also not change your IIS script mappings the same way the upgrading from .NET 1.1 to .NET 2.0 did.Obviously, if you are working on an

css and mvc2

Hi,Is it just me or is mvc 2 just not that friendly when it comes to working with css? Say i want to make one particular page with a different color background from the rest. Well, i can't just embed within that page the css rule as there is no <head> in the aspx page. The only option is to make a new .master page just for that page, and a new css page attachment to that master page. So i've had to create 2 files just so i have the freedom of working on a particular aspx page with css.Is this right or is there simpler way to add css to aspx pages?

Visual Studio 2010, MVC2.0, IIS7, .NET4 deployment problem

I have a problem where I can get an MVC2.0 application using .NET3.5 running in IIS, but when I upgrade it to .NET4.0, the browser just reports error 500 Internal Server Error. The problem is easy to replicate as follows.  Firstly, to show it works with .NET3.5: Create a new MVC2.0 application in Visual Studio 2010, using the inbuilt starter template. Run the application using the development server, and the Home & About pages display fine. Publish the application to a directory. Copy the directory to a subdirectory under inetpub/wwwroot on my Vista computer, running IIS7 Create a new application in IIS Manager & select the DefaultAppPool (integrated pipeline) Browse the application and Home & About pages are displayed. Then the problem I have is after upgrading to .NET4.0: Change to .NET4.0 for the project in Visual Studio Build & this runs fine with the development server. Publish the application & set up IIS as before, except choosing the application pool ASP.NET v4.0 (integrated pipeline) Browse the application, and I get the 500 Internal Server Error I'm guessing there is some extra configuration required - can anyone help? I've deliberately used the default MVC installation to eliminate any variables caused by my programming .. Thanks in advance, Steve

Migrating MVC1 application to MVC2

I am having a Asp.net MVC1 based application which needs to be migrated to Asp.net MVC2. I opened this application in vs2010 and it automatically migrated it to MVC2. However one of its code line is creating problem during page exceution. I have following line in Application_start funtion of Global.asax ModelBinders.Binders.DefaultBinder = new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder(); This is because Microsoft.Web.MVC.DataAnnotations dll is missing in MVC2. I tried to skip this error by commenting this code but that is also giving error. Can somebody please guide me about its replacement code. Thanks in advance Cheers TicArch

How to make a default Area in MVC2??

I have the area Blog with controllers (Post and Blog)i want that when the user acess my site without one area, it should redirect him to the Blog area inthe blog controller and in the index actionif i use:            routes.MapRoute(                "ClearPath",                "",                new {area = "Blog",controller = "Blog", action = "Index" }            );i got one error cause he search the view in the root folder: The view 'Index' or its master was not found. The following locations were searched:~/Views/Blog/Index.aspx~/Views/Blog/Index.ascx~/Views/Shared/Index.aspx~/Views/Shared/Index.ascx

MVC2 NerdDinner - Data no longer saves after adding ViewModel

I am writing this post to document some issues I ran into, and hopefully help anybody else who gets stuck in the same place.  I am creating the NerdDinner application via the Professional ASP.NET MVC 2 book,  and once I got to the ViewModel part (pages 88-91), I noticed none of my data was saving any longer.  I discovered several errors and areas of confusion in these pages of the book, which I will list now. Page 88:  When it says to add the DinnerFormViewModel class, it doesn't say what folder to add it to.  On page 89, the Inherits example (NerdDinner.Controllers.DinnerFormViewModel) implies it was added to the Controllers folder, but if that were so, the code on page 88 would need to use Models.Dinner instead of just Dinner.  No big deal; just put it in Models and change the Inherit statement on page 89 to use NerdDinner.Models.DinnerFormViewModel. Page 89:  At the bottom of the page is some wrong code.  Since the properties of Dinner are now one level deeper, all references to m.Title, m.Country, etc, need to be m.Dinner.Title, m.Dinner.Country, etc.  The statements above the code are also incorrect ("Notice... we are not changing the names of the input elements... the form elements will still be named...") -- the form elements will actually change from "Title", "Country&quo
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend