model-binding

ModelBinding in asp.net mvc Beta1

I converted my web application from preview 3 to beta1 and am now trying to put the new functions of the framework to use. One of them is ModelBinding. For this particular situation I created a class that is (for now) just a container of a bunch of simple-type properties. If I create a form with a bunch of textboxes, I want the framewor...

Html.BuildUrlFromExpression with reference type parameters for action

I hope my terminology is right. Edit if not. From my Linq2Sql classes I have a Color class. One of my controller's actions accepts an instance of this Color class. I want to create a link to this action so I use <%=Html.ActionLink<ColorController>(c=>c.Details(ViewData.Model.ActiveColor), "test")%> Where ViewData.Model.ActiveColor ...

How to pass an unpersisted modified object from view back to controller without a form?

Short: how does modelbinding pass objects from view to controller? Long: First, based on the parameters given by the user through a search form, some objects are retrieved from the database. These objects are given meta data that are visible(but not defining) to the customer (e.g: naming and pricing of the objects differ from region to ...

Modelbinding with SelectList

I create a DropDown with the Html.DropDownList(string NameSelectListInViewData) method. This generates a valid Select input with the correct values. And all is well. Upon submit however, the value in the source SelectList is not bound. Case: ViewData.SearchBag.FamilyCodes: public SelectList FamilyCodes { get; set; } Html that gene...

System.StackOverflowException was unhandled (MVC Model BINDING)

I started getting this error when posting the form back with Model Binder. To test the problem I reduced the postback to one string property of the model but i still get the overflow error. Can anyone suggest what would cause this? UPDATE The problem appears to be related to the property in the model that is a foreign key. If this key...

Asp.NET MVC Model Binding - Using Bind Parameter Attribute to assign a prefix for a simple type

I have a .net mvc app with an controller action which accepts user registration posts. I have the following UI fields: emailaddress, firstname, lastname, password, and confirmpassword. Some of these fields do not belong to a model object (i.e. confirm password does not belong to the user model, only password). My registration form resi...

ASP.NET MVC model binding an IList<> parameter

[I solved this myself, see my answer for cause] I'm having trouble getting form values for a IList<> argument in a controller method set properly. My controller class looks like this: public class ShoppingBasketController : Controller { public ActionResult Index() { return View(); } [AcceptVerbs(HttpVerbs.Post)] ...

ASP.NET MVC model binding foreign key relationship

Is it possible to bind a foreign key relationship on my model to a form input? Say I have a one-to-many relationship between Car and Manufacturer. I want to have a form for updating Car that includes a select input for setting Manufacturer. I was hoping to be able to do this using the built-in model binding, but I'm starting to think I'...

Model binding and GET requests?

There are tons of examples for model binding in html forms, but I'm wondering if its possible to, and if so how to, use model binding for ActionLinks/GET requests. So, given the following model public class Lurl { public string Str {get;set;} public char Chr {get;set;} public double Dbl {get;set;} } and the following route (I'm...

Custom IModelBinder and Database Access

For a user object in my asp.net mvc project, I have written a custom modelbinder to check whether passwords are valid and whether two matching passwords were entered etc.. The login name needs to be unique though, I was wondering whether I can check for that in the modelbinder, or is this considered bad practise ? The thing is that th...

More complex(real-life) modelbinding?

Roll with me and imagine the following example: Public ViewResult GiveMeFruit(int personId, string personName, int personAge, int fruitId){ Person person = PersonService.GetPerson(personId); person.Name = personName; person.Age = age; person.Fruits.Add(FruitService.GetFruit(fruitId)); ViewData.Person = person; Vi...

Dynamic table row not being updated via jquery ajax call

Hi there, Having a bit of a hairy issue when submitting data over a jquery ajax call. Basically, form values of form fields which have been dynamically added to the screen into a tubular grid using jquery are not updating on a batch save command. Basically I have a grid listing which displays all current rows saved to the database with...

How to use TryUpdateModel in this context?

In my querystring I get a bunch of parameter names and values. As I understand I should be using the built in asp.net mvc function TryUpdateModel(modelInstance). It seems though that it is not working as I'm expecting. My parameter names do defer in capitalization. Is this a problem? Furthermore I have some custom types that need a speci...

ASP.NET MVC Model Binding

I'm having a problem with model binding. If I do use the prefix in the CopyToModelStateDictionary method, I don't get the styling or validation messages for the invalid controls. If I don't use the prefix, I get the styling and validation messages, but the page crashes if the user hasn't made a selection from a dynamically created list o...

Model Binding on Multiple Model Form Submission from Strongly-Typed View

...

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

How to do Model Binding with Jquery Ajax

Hi, I'd like to use model binding to keep my controllers looking cleaner, you can see how much nicer it is using model binding: public ActionResult Create(Person personToCreate) { //Create person here } vs public ActionResult Create(string firstName, string lastName, string address, string phoneNum, string email, string postalCo...

Is it bad practices to allow null fields to a DB Table in order to simplify Model Binding from Ajax?

Please read here and here to get a quick overview of my problem and to see exactly what I mean when I say Model Binding from Ajax. Would it be a bad idea to make the foreign key fields nullable in order to allow for Model Binding from javascript? For example, we want to bind to a Person object during an ajax call to... (The Person cla...

how do you go about dealing with ajax and Model Binding when complex types are involved?

I would like to keep model binding when doing ajax calls from my view. I'm using jquery ajax to make the calls, and just as an example- this is my controller method I want to call from ajax: public ActionResult Create(Person personToCreate) { //Create person here } As you can see, this method is relying on Model Binding. This make...

MVCContrib grid and posting back with model binder

The contents of my MVCContrib grid come from the Model on a strongly typed View. When a post is made, the contents of the grid are not in the model object when it returns to the controller. I can see that this is because the grid renders as just a table with text in cells. Is there something I can do so that when the post occurs, the li...