modelbinders

ASP.NET MVC 1.0 - Model binder for Dictionaries

I have a ViewModel class containing a Dictionary (and other irrelevant things for this question): public class MyViewModel { public Dictionary<int, string> Data { get; set; } /* ... */ } Then I have a couple of GET/POST actions that deal with the dictionary. The GET action will first fill the Dictionary<int, string> with some ...

DDD-friendly ASP.NET MVC Model Binder?

I'm considering the value of a custom model binder that can instatiate immutable value objects defined in my domain layer. Then I can just pass them through the stack and set them on the appropriate entity. Has anyone tried? Had any luck? Think its a silly idea? ...

How do I copy copy all data in a custom modelbinder?

How do I iterate through all data and copy it to my item? public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { try { IRepository<ContentItem> repository = ObjectFactory.GetInstance<Repository<ContentItem>>(); var itemId = bindingContext.ValueProvider.GetValue("CurrentItem...

Why does ASP.NET MVC care about my read only properties during databinding?

I have a read only property on my 'Address' model 'CityStateZip'. It's just a convenient way to get city, state, zip from a US address. It throws an exception if the country is not USA (the caller is supposed to check first). public string CityStateZip { get { if (IsUSA == false) { ...

Complex Strongly-Typed Object + Default ModelBinder

Hi everyone, I have the following complex model: Category - has -> List which has -> List I am trying to make a create action where I can pull all the existing categories and insert for each category one List all together, including the sub items. For example: -Cat1 -- NewItemGroup1 --- NewItem1 --- NewItem2 --- NewItem3 --- ...

What is the difference between BindProperty and SetProperty on IModelBinder

I'm creating a custom model binder in an Mvc application and I want to parse a string to an enumeration value and assign it to the model property. I have got it working overriding the BindProperty method, but I also noticed that there is a SetProperty method. protected override void BindProperty(ControllerContext controllerContext,...

DefaultModelBinder Problem with nested levels + other binders

I have what I would think is a somewhat normal situation where I need to bind form posts to an "order" model. This model has a few levels of information to it: Order.Billing.FirstName Order.Billing.Address.City Order.Billing.Address.Country Using the DefaultModelBinder, if I POST a form to an action that takes this Order model as the ...

Binding Data to an Existing Object in Asp.net MVC

Within an ASP.Net MVC model binder is it possible to create an object of the bound type and then update the properties on it. e.g. public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { ParentType boundModel = null; if (bindingContext.ModelType == typeof(ParentType)) ...

How to update custom ModelBinder to work with altered ModelBindingContext.ValueProvider interface in ASP.NET MVC RC2

I have a custom model binder that takes a comma separated list and cleans out any empty values, then passes it along to the default model binder. This worked in ASP.NET MVC Preview 2, but when I upgraded to RC2, the below won't compile because the interface of ValueProvider only has a GetValue() method, no [] accessor. Is what I'm doing ...

Is there a way to have the DefaultModelBinder ignore empty items when binding to a List<Enum>

I have a scenario where I'd like to change the behavior of the DefaultModelBinder in how it binds to a List of enums. I have an enum... public enum MyEnum { FirstVal, SecondVal, ThirdVal } and a class for a model... public class MyModel { public List<MyEnum> MyEnums { get; set; } } and the POST body is... MyEnums=&MyEnums=Thi...

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 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...

MVC2 Modelbinder for List of derived objects

I want a list of different (derived) object types working with the Default Modelbinder in Asp.net MVC 2. I have the following ViewModel: public class ItemFormModel { [Required(ErrorMessage = "Required Field")] public string Name { get; set; } public string Description { get; set; } [Scaffold...

Attribute based validation in ASP.NET Web Forms using MVC libraries?

Is there a way in ASP.NET Webforms to accommodate attribute based validation. If so any urls where this has been demonstrated. I am not looking for open source projects. Just as you can use MVC Routing in Web Forms, can the validation framework be used? ...

asp.net MVC 1.0 and 2.0 currency model binding

I would like to create model binding functionality so a user can enter ',' '.' etc for currency values which bind to a double value of my ViewModel. I was able to do this in MVC 1.0 by creating a custom model binder, however since upgrading to MVC 2.0 this functionality no longer works. Does anyone have any ideas or better solutions fo...

ASP.NET MVC - POST Action Method with Additional Parameters from URL

With ASP.net MVC is it possible to POST a form to a controller action which includes parameters not in the form, but from the URL? For example The Action method in GroupController: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(int idOne, int idTwo, Model model) { ... } The route: "{controller}/{action}/{idOne...

ASP.NET MVC 2 - ViewModel Prefix

I want to use RenderPartial twice in my view with different models associated. The problem is that some properties are present in both models (nickname, password). They have no prefix, so even the id's or names are equal in the output. Now, if I have model errors for nickname or password, both fields get highlighted. Main View: <div> ...

Multiple custom model binders for one model in ASP.NET MVC

This is a general model binding question that applies to MVC 1 & 2. I'm wondering if MVC2 would be better for this, but here's my question: I have a fairly complex model: public interface IEvent public int Id public string Title public List<EventContact> Contacts public List<EventDatesLocations> DatesLocations public class Ev...

.NET-MVC Modelbinder not binding because of primary key, all other fields are right. What's happening?

I want to perform an Add object scenario in .NET using the modelbinder. Function AddObject() As ActionResult Return View() End Function <AcceptVerbs(HttpVerbs.Post)> _ Function AddObject(<Bind(Exclude:="ObjectId")> ByVal o As BLL.Object) As ActionResult o.Save() End Function However, I get an error: 'o.ObjectId' is not de...