custom-modelbinder

ASP.NET MVC2 - Custom Model Binder Examples

I am trying to find some examples of building a custom model binder for a unique binding scenario I need to handle, but all of the articles I found were for older versions of MVC which are no longer relevant in MVC2. I've been referencing the DefaultModelBinder source code to try to get a general feel for what I need to do, but it's enti...

ASP.NET MVC2 - Resolve Parameter Attribute in Model Binder

Given an action like: public ActionResult DoStuff([CustomAttribute("foo")]string value) { // ... } Is there any way to resolve the instance of value's CustomAttribute within a ModelBinder? I was looking at the MVC sources and chances are I'm just doing it wrong, but when I tried to replicate their code which retrieves the BindAttrib...

ASP.NET MVC - Custom model binder able to process arrays

I need to implement a functionality to allow users to enter price in any form, i.e. to allow 10 USD, 10$, $10,... as input. I would like to solve this by implementing a custom model binder for Price class. class Price { decimal Value; int ID; } The form contains an array or Prices as keys keys: "Prices[0].Value" "Prices[0].ID" "Pr...

Custom DateTime model binder in Asp.net MVC

I would like to write my own model binder for DateTime type. First of all I'd like to write a new attribute that I can attach to my model property like: [DateTimeFormat("d.M.yyyy")] public DateTime Birth { get; set,} This is the easy part. But the binder part is a bit more difficult. I would like to add a new model binder for type Dat...

Model Binding, a simple, simple question

I have a struct which works much like the System.Nullable type: public struct SpecialProperty<T> { public static implicit operator T(SpecialProperty<T> value) { return value.Value; } public static implicit operator SpecialProperty<T>(T value) { return new TrackChanges<T> { Value = value }; } ...

DataAnnotations validation and custom model binder

I have an action method that accepts the following model - LanguagesViewModel: public class LanguagesViewModel : ViewModelBase { IEnumerable<LanguageItem> Languages { get; set; } } public class LanguageItem { [Required] public int LanguageId { get; set; } [Required] public int SpeakingSkillId { get; set; } [Required] public in...

Can I register a custom model binder somewhere other than Global.asax?

It would be handy to limit the scope of a custom model binder for just a specific controller action method or its entire controller. Hanselman wrote a sentence that implied alternative locations for custom model binder registration but never seemed to finish the thought: You can either put this Custom Model Binder in charge of all yo...

ASP.NET MVC routing issue with Google Chrome client

My Silverlight 4 app is hosted in ASP.NET MVC 2 web application. It works fine when I browse with Internet Explorer 8. However Google Chrome (version 5) cannot find ASP.NET controllers. Specifically, the following ASP.NET controller works both with Chrome and IE. //[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")] public...

custom modelbinder

Hi I want to create a custom modelbinder which validates the bounded model. I have found several examples of this and it works as it should. But I want to also to be able to send the user back to the page he came from if there is errors in the model. Is this possible to do and are there any obvious side effects by doing this? What I w...

How do I invoke UpdateModel from within a Custom ModelBinder? (MVC)

Hi, I'm creating a few custom binders for complex types in my Model. My model is composed of objects that have their own separate binders. I want the base object to do its dirty work and then populate the complex object it encapsulates by passing off to the standard ModelBinder routing. How do I do this? For illustration purposes I'...

Saving Dropdown list selection with Entity Framework in ASP.NET MVC solution

Hi, I'm looking for advice on a decent pattern for dropdown list selection and persistence of the selection with POCO EF please. I have a list of IEnumerable<Country> in my view model where Country is a POCO loaded via EF. There is an Address property on the view model that takes the current or user selected value on it's Country prop...

Getting the Action during model binding

Hi, Is there a way of getting the Action, and reading any attributes, during the model binding phase? The scenario is this: I've got a default model binder set-up for a certain data-type, but depending on how it's being used (which is controlled via an attribute on the action) I need to ignore a set of data. I can use the RouteData o...

ASP.Net MVC ModelBindingContext class-- how are its model values populated?

I'm scratching my head a bit at how model binders do their work in ASP.Net MVC. To be specific, the BindModel() method has a ModelBindingContext parameter that holds the model name and type, but I don't understand how the ModelBindingContext receives these values. An MVC model has to be populated from posted form values or query st...

ASP.NET MVC - Custom Model Binder for ID fields

Hi, i have the following entities: public class Category { public virtual int CategoryID { get; set; } [Required(ErrorMessage = "Section is required")] public virtual Section Section { get; set; } [Required(ErrorMessage = "Category Name is required")] public virtual string CategoryName { get; set; } } public class...

Custom Model Binder for DropDownList not Selecting Correct Value

Hi, i've created my own custom model binder to handle a Section DropDownList defined in my view as: Html.DropDownListFor(m => m.Category.Section, new SelectList(Model.Sections, "SectionID", "SectionName"), "-- Please Select --") And here is my model binder: public class SectionModelBinder : DefaultModelBinder { public override ...