modelbinders

Preventing a model binder from returning a list of null objects in ASP.NET MVC

I have a pretty complex object with numerous datatypes. A few datatypes are Lists (LazyLists to be exact). In my form, I let the user enter up to, say 3 items for this list and my form input names correspond appropriately: myObj[0].Name myObj[0].Email ...consectuviely... myObj[2].Name myObj[2].Email If the user decides to only enter...

Asp.net mvc 2 model binders what's behind scene

Hello, Im starting with MVC2 and i have a simple question: If i have a typed view with a form inside, and this textbox created with lambda expressions: <%: Html.TextBoxFor(e => e.Name)%> When i submit this form the default model binder take the request form, takes the model typed to the view, serialize the data posted (as a this m...

Help passing objects to methods in asp.net mvc 2

I have this snippet in my html... Fusion Charts requires I feed it an XML to create a graph <script type="text/javascript"> var myChart = new FusionCharts("/Content/Fusion_Charts/Charts/Column3D.swf", "myChartId", "470", "350", "0", "0"); myChart.setDataURL("/XML/Graph/?list=<%=Model.list%>"); myChart.render("Gra...

Using a custom model binder for an argument of a controller action

I have a controller action that looks like: public ActionResult DoSomethingCool(int[] someIdNumbers) { ... } I would like to be able to use a custom model binder the create that array of IDs from a list of checkboxes on the client. Is there a way to bind to just that argument? Additionally, is there a way for a model binder to d...

ASP.NET MVC2 : DateTime modelbinding via HTTP GET

I'm getting some trouble binding a date from QueryString : I have the following model public class QueryParms { public DateTime Date { get; set; } } And the following controller action : public ActionResult Search( QueryParms query ); I have a form, with a field where I can type my date. If the form is FormMethod.Post, everyt...

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

ModelBinder NHibernate Complex Object with Collections

Below, on initial load (Edit), everything displays fine. However, when I POST (Action below), it looks like it goes and tries to grab the ProductAttribute models separately, with the Product's id, which promptly fails. How to keep my Binder implementation from trying to re-bind Collections as separate entities? Thanks! Model public ...

asp.net mvc client side validation for camel case property name

Hi all, I am using the MS shipped client side validation in asp.net mvc 2. The model in question has one property called "FirstName". Our client side developer really like to have camel-case in the elements id, so instead of using the normal html helper Html.TextBoxFor(m => m.FirstName), we wrote out the html input view instead like: ...

Model Binder in ASP.NET webforms

For number of years I did ASP.NET web forms development I was spoiled by a proprietary library, which allowed me to do things like: UpdateToObject(ControlsCollection, obj) UpdateFromObject(ControlsCollection, obj) Conceptually code did something very similar to what MVC Model Binder does, i.e. given form's posted values as inp...

Custom ModelBinder for IEnumerable post result in MVC

Consider this view model that uses two custom validators. It posts back to the controller in an enumerable collection. public class FormFieldViewModel { public Guid FormFieldKey { get; set; } public string Name { get; set; } // Utilize a string[] array since multiple-selection items will require this (we can assume array[0]...

Model Binding property with implicit conversion from string to type

I have the following class: public class PostCode { public string Name { get; set; } public static implicit operator PostCode(string postCode) { return new PostCode {Name = postCode}; } } It forms part of an Address class which is the model for a helper template (EditorTemplates>Address.ascx). This helper tem...

Generic TimeSpan binding in Asp.NET MVC 2

I have an input form that is bound to a model. The model has a TimeSpan property, but it only gets the value correctly if I enter the time as hh:mm or hh:mm:ss. What I want is for it to capture the value even if it's written as hhmm or hh.mm or hh.mm.ss or ... I want many different formats to be parsed correctly. Is this possible? Thank...

Testing custom ModelBinder against HTTP context in ASP.NET MVC (1.0)

Hi, I'm trying to unit test a custom model binder - specifically, I want to see how it responds to various (possibly conflicting) values being submitted in the Request.Form and Request.QueryString collections - i.e. if I submit one value in the form and another in the querystring (yeah, yeah, I know, this is evil, but I want test covera...

ASP.NET MVC Model Binding IList in an Editor Template

I am attempting to bind a list that is part of a larger view model without resorting to a custom model binder. When I use an editor template to build the list of inputs, the generated names are not in the correct format for the default binder to work. Instead of Items[3].Id like I would expect it is Items.[3].Id. If I build the list wi...

Data annotations modelbinder in new mvc

Hi we are trying to upgrade our project to work in the latest version of VS 2010 and i have encountered a problem with the old Data Annotations Modelbinder. Dim modelBinder = New DataAnnotationsModelBinder() modelBinder.InvalidValueFormatter = _ Function(propDescriptor As PropertyDescriptor, value As String, displayName As String) _ ...