modelbinders

How to Unit Test a custom ModelBinder using Moq?

I'm having some difficulty writing some Unit Tests to test a custom ModelBinder that I created. The ModelBinder I'm trying to Unit Test is the JsonDictionaryModelBinder that I posted here. The problem I'm having is getting the Mocking all setup using Moq. I keep getting Null Exceptions due to the HttpContextBase not being Mocked correct...

ASP.NET MVC: Binding numeric types from blank strings without ModelState errors

I have a fairly complex ViewModel containing decimal properties, which are exposed to the user in the form of text boxes. I want a textbox with no value to be interpreted as zero. (The properties in the underlying domain object are non-nullable, and the default value is 0.) When the DefaultModelBinder binds the view data to the ViewMode...

Updating Parent/Child Records with model binders in ASP.Net MVC

I have modified the Nerd Dinner application to allow editing of child records by adding the following code to the DinnerForm.ascx <p> <%int i = 0; foreach (NerdDinner.Models.RSVP rsvp in this.Model.Dinner.RSVPs) { %> <%= Html.Hidden("Dinner.RSVPs[" + i + "].RsvpID", rsvp.RsvpID)%> <%= Html.Hidden("Dinner...

How to use two instances of the same .ascx in the same page in ASP.NET MVC ?

I have two instances of an Address.ascx control in an ASP.NET MVC page. <h1>Shipping Address</h1> <% Html.RenderPartial("Controls/AddressControl"); %> <h1>Billing Address</h1> <% Html.RenderPartial("Controls/AddressControl"); %> Of course, with the code exactly like this I'll end up with the same IDs for each field in the...

If I need to retrieve an object from a custom model binder should the binder interact with the service layer, the repository layer, or ...? Asp.Net MVC

If I have a class similar to this: public class Person { public string firstName { get; set; } public string lastName { get; set; } public Pet myPet { get; set; } } When I create a custom model binder, the Post from my form will not be sending in a Pet, it would send in data like this: firstName: "myFirstName" lastName: "m...

ASP.NET MVC - Testing Page to Model Binding

Hello, I have unit tests covering my model binders. I create a ModelBindingContext and populate the ValueProviderDictionary with my test values. I feel confident that once my controller gets the model, everything is covered with testing and the right things are happening. I also feel confident that if the BindingContext is correct my...

dropdown value null when using, viewmodel & modelbinder in asp.net mvc

I am using asp.net's modelbinder functionality to bind form values to my entity when posting from a view. The html renders correctly in the initial view with correct option and value items. When completing the form and posting, all values are populated correctly into the entity except the value from the dropdown list. not sure what I ...

Modelbinding for empty query string parameters in ASP.NET MVC 2

The behavior described here appears to now be the default for ASP.NET MVC 2 (at least for Preview 1). When modelbinding a querystring like this : ?Foo=&Bar=cat The following binding occurs (assuming you're binding to a model with 'Foo' and 'Bar' string properties) ASP.NET MVC 1 model.Foo = ""; model.Bar = "cat": ASP.NET MVC 2...

MVVM and ModelBinders in the ASP.NET MVC Framework

Hi, I've got a series of views, each are typed to have their own ViewModel class which contains everything they need to display themselves, for example: public class CreateResourceViewModel { public Project Parent { get; set; } public SelectList Categories { get; set; } public Resource Resource { get; set; } } The post...

ASP.NET MVC, LINQ and ModelBinders...

Hi all, Is there a pre-built ModelBinder I can use with LINQ to get an object from a DataContext and update it on a HTTP post? For example, currently I have this block of code: [AcceptVerbs (HttpVerbs.Post)] public ActionResult Edit (Project project) { var projectService = Factory.GetService<IProjectService> (); project = proj...

How [and where] implement a validation using ModelBinder.

Hello. I'm developing a little site using ASP.NET MVC, MySQL and NHibernate. I have a Contact class: [ModelBinder(typeof(CondicaoBinder))] public class Contact { public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual int Age { get; set; } } And a Model Binder: public class Contac...

Extending Sanderson's custom mvc ModelBinder for an object stored in session

In his wonderful MVC book Steven Sanderson gives an example of a custom model binder that sets and retrieves a session variable, hiding the data storage element from the controller. I'm trying to extend this to cater for a pretty common scenario: I'm storing a User object in the session and making this available to every action method a...

ASP.NET MVC UpdateModel with interface

I am trying to get UpdateModel to populate a model that is set as only an interface at compile-time. For example, I have: // View Model public class AccountViewModel { public string Email { get; set; } public IProfile Profile { get; set; } } // Interface public interface IProfile { // Empty } // Actual profile instance used publ...

ASP.NET MVC Issue with Using Reflection Created Objects with the Default Model Binder

I am having a weird issue in ASP.NET MVC with objects not being updated with UpdateModel when passed a formCollection. UpdateModel does not appear to be working properly when the object being updated is created through reflection. Scenario: I have an application which has approximately 50 lookup tables--each of which includes exactly th...

model binding in mvc

i'm having a textbox inside a form. [View] <%=html.textbox("name") %> [Controller] Index(string name) { name = "something"; return View(); } On Form Submit In this case without sending any ViewData the textbox value is maintained.But the value "something" is not setting up. But whn i change the Action to [Cont...

MVC Model Binding: Using a name different from the QueryString for the Method Parameter

Given a URL: http://www.stackoverflow.com/question?ask=123&amp;answers=5 and its corresponding ActionMethod and Model: public ActionResult Question(RequestObject request) { return View("Question", request); } public class RequestObject { public string AskId { get; set; } public string NumberOfAnswers ...

How does MVC Invoke an Action and map Request.Form to it's Parameters

I need to create something similar to how MVC invokes an Method(Action) and also uses the Model Binder to map a NamedValueCollection to the parameters on that method. Basically I have a Controller action that needs to dynamically call a method on a class, the controller has any information sent in a form or query string plus the name of ...

Get FormCollection out controllerContext for Custom Model Binder

I had a nice function that took my FormCollection (provided from the controller). Now I want to do a model bind instead and have my model binder call that function and it needs the FormCollection. For some reason I can find it. I thought it would have been controllerContext.HttpContext.Request.Form ...

Some Fields Not Coming Through In ModelBinder

The following is the code for my controller: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int id, Actor actor) { try { actorRepository.Save(actor); return RedirectToAction("Index"); } catch { return View("Edit"); } } The view...

ASP.net MVC v2 - Debugging Model Binding Issues - BUG?

Hi guys I am having more than a little difficulty trying to debug why MVC is not binding correctly in a given case I have... Basically, I have my action which receives a complex object which in turn has a complex child object - Activity.Location.State (Where Activity is the complex object that the action expects, Location is a complex ...