Custom Model Binder Option In MVC 2 RC - How To Iterate Over Form Values
Feb 2, 2010I'm using the MVC 2 RC. I have a custom model binder that used to iterate over posted form values in MVC 1.0 like this:
[Code]....
I'm using the MVC 2 RC. I have a custom model binder that used to iterate over posted form values in MVC 1.0 like this:
[Code]....
Is it possible, inside a Custom Model Binder, to fire "something" that "says" the value is invalid so it gets handled by validation part?
Basically, I am getting an exception when the value for the property is invalid.
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 object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (bindingContext.ModelType.IsAssignableFrom(typeof(Section)) && value != null)
{
if (Utilities.IsInteger(value.AttemptedValue))
return Section.GetById(Convert.ToInt32(value.AttemptedValue));
else if (value.AttemptedValue == "")
return null;
}
return base.BindModel(controllerContext, bindingContext);
}
}
Now within my controller i can say:
[HttpPost]
public ActionResult Create(FormCollection collection)
{
var category = new Category();
if (!TryUpdateModel(category, "Category")
return View(new CategoryForm(category, _sectionRepository().GetAll()));
}
This validates nicely and the correct value for the section is assigned when the model is updated, however it does not select the correct value if another property doesn't validate.
I am trying to make a post that should use the Default Model Binder functionality in ASP.NET MVC 2 but unfortunately I can't get through. When I click on the checkout button I populate a form dinamically using jQuery code and then submit this form to the server. This is the form that get submitted
<form action="/x/Order/Checkout" id="cartForm" method="post">
<input name="__RequestVerificationToken" type="hidden" value="UDjN9RdWheKyWK5Q71MvXAbbDNel6buJd5Pamp/jx39InuyYIQVptcEubIA2W8DMUzWwnZjSGkLspkmDPbsIxy8EVuLvfCSZJJnl/NrooreouptwM/PaBEz2v6ZjO3I26IKRGZPqLxGGfITYqlf8Ow==">
<input id="CustomerID" name="CustomerID" type="hidden" value="1">
<input id="FirmID" name="FirmID" type="hidden" value="2">
<input type="hidden" name="CartItems[0].ServiceTypeID" value="1">
<input type="hidden" name="CartItems[0].Quantity" value="1">
<input type="hidden" name="CartItems[1].ServiceTypeID" value="2">
<input type="hidden" name="CartItems[1].Quantity" value="1">
</form>
This is the jQuery code that handle the submit event for the form
$("#cartForm").submit(function (event) {
event.preventDefault(); var form = $("#cartForm");
var panel = form.parent(); panel.parent().block();
$.ajax({ type: "post", dataType: "html",
url: '<%: Url.Content("~/Order/Checkout") %>',
async: false, data: form.serialize(),
success: function (response, status, xml) { panel.parent().unblock(); },
error: function (response) { panel.parent().unblock(); } }); });
This is the controller action that should be get called
[HttpPost]
[ValidateAntiForgeryToken]
public virtual ActionResult Checkout( CartModel cart ) {
} And finally this is the CartModel class involved
public class CartModel : BaseModel{
public int CustomerID { get; set; }
public int FirmID { get; set; }
public List<CartItemModel> CartItems { get; set; }
public CartModel() { CartItems = new List<CartItemModel>();
} } public class CartItemModel : BaseModel
{ public int ServiceTypeID { get; set; }
public int Quantity { get; set; } }
But the default Model Binder does not bind the web form data to a CartModel class. Using Fiddler I have been able to see that the data sent to the server is correct as you can see from the following snapshot.
What I'm trying to do is rather basic, but I might have my facts mixed up. I have a details page that has a custom class as it's Model. The custom class uses 2 custom objects with yet another custom object a property of one of the 2. The details page outputs a fair amount of information, but allows the user to post a comment. When the user clicks the post button, the page gets posted to a Details action that looks something like this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Details(VideoDetailModel vidAndComment) { ....}
[Code]....
The only fields on the form that is posted are CommentText and VideoId. Here is what the VideoDetailModel looks like.
public class VideoDetailModel
{
public VideoDetailModel()
{
[Code]....
I suppose if I added more form fields for the properties I need, they would get posted, but I only need 1 form entry field for the CommentText. If I could get the same Model objects value that were sent to the page to post with the page, it looks like the solution is rather simple. I think using the RenderPartial in the middle of a form is problematic somehow to how the form gets written in html. I can't really put my finger on why things went bonkers, but if I do my RenderPartials before my form and then begin my form with the text entry field and the hidden VideoId, the default ModelBinder works just fine. I was beginning the form, writing the hidden VideoId, rendering several partial views, create my CommentText field, and then closed the form out. The CommentText field would get bound just fine. The hidden VideoId would not. Maybe I missed a rule somewhere about using RenderPartial.
For completeness, the partial view I was rendering took a Comment object and just wrote out it's CommentText data. Several of these objects would exist for a single Video object. All of this data was in a custom type and passed into the View (the main view) as it's Model. This partial view did not have a form and did not have any data entry fields.
I have a control that's bound to a list property. If I remove elements from that list using javascript and post, then the model gets updated, and the elements get removed.
Unless I remove all the items from the list. Then no names for that property go into the form data, and so the default model binder leaves the model's list untouched. This code from DefaultModelBinder.cs line 572 in the RTM sources makes it clear:
[Code]....
So how do I indicate to the model binder that I do want it to update the list and Ido want the list to be emptied?
Given the following view model and action using the DefaultModelBinder, it seems to ignore the dictionary, but bind all other properties correctly. Am I missing something here? Looking at the MVC source code this seems legit.
public class SomeViewModel
{
public SomeViewModel()
{[code].....
I need to bind a bunch of properties over my model entities. All of them uses the List<T> class. I already managed to write a model binder that can treat individualy types derived from that class, but i can't set the value of this property on the model. every time i check the model afer the bind process i see a list with 0 itens.
Here's how it runs.
After i post the values the model binder catchs up the types for bindingAt the custom model binder i check if this property is a List<T> typeIf it is then i perform the bind like it have to be, if not i let the default binder do the job.Finally i return the object binded. What happens next is the issue i've mentioned "i see a list with 0 itens" on the Model property.
Here is the code of Custom Model Binder:
[Code]....
I'm using MVC 2.0 in an ASP.NET application using NHibernate. I have a working View, Controller and data access layer using NHibernate that is able to display and save an entity with a relationship to another mapped entity: Person -- > Location It's using the HTML helper HTML.DropDownListFor() to display a list of all Locations. The user is able to select one of the Locations from the list, and press save. The default model binder correctly sets the value of the Location on the Person entity being saved. This location is an nhibernate mapped entity, and is instantiated and has the id value that was selected in the dropdown list. Obviously, since the dropdown list that holds locations only has the ids of the locations, the rest of the values for the location are null. This is OK. I am only trying to save the Person with a reference to an existing location.
So, here comes the complication. We have a need to change the relationship between the two entities. Now the Person can have a reference to many locations. Person.Locations will be an IList My question is, how do you get the default model binder to take selections from a multiselect dropdown and populate an IList. I've managed to save collections of entities in the past using the syntax [index].PropertyName as explaing by Phil Haacked .... [URL]
The issue here is that I have only a dropdown list, and it will post back to the modelbinder a repeating key with different values:
Person.Location.Id: 2
Person.Location.Id: 4
Person.Location.Id: 5
This, unfortunately, doesn't work. the Location list keeps coming back Null. Our UI guy is using a slick JQuery pluggin to display the items in the select list, so I'd rather not have to use a different UI.
I saw a post with posibble problems with Model Binding which is mentioned here. [URL]. Whats the best approach to this? Different approaches are below.
[Code]....
I have a complex page with several forms on it. The page is divided into sections, and each section has a continue button on it. The page is bound to a pageViewModel, each section addresses a different set of properties on the model. The continue button makes an ajax call to the controller, and the model binder binds it appropriately to the appropriate sections of the model. The section is refreshed appropriately. Finally, I would like to have a save button at the bottom of the page that takes all the forms, and binds all of the forms to the model. The model, at this point has all of the properties filled out, and can be processed accordingly. Can I accomplish this by some ASP MVC magic?
View 2 RepliesI have a section of a form that I need to handle differently from the rest of form results. In the section that needs special handling I need to iterate over 3 form fields that have the same name. They have to have the same name, I can't change it. The section of the form I am referring to looks something like this:
<td><input name="Color" size="20" value="" type="text"></td>
<td><input name="Color" size="20" value="" type="text"></td>
<td><input name="Color" size="20" value="" type="text"></td>
Using C# I try something like this:
I try to handle it like this:
int i;
for (i = 1; i <= Request.Form["Color"][i]; i++)
{
colorName.Text += Request.Form["Color"];
}
Which leads to the following exception:
System.NullReferenceException: Object reference not set to an instance of an object.
How should I be handling form fields with the same name?
I have created a Search page inside the Home section and is handled by the HomeController.Inside the HomeController I created a custom ActionResult called Lookup():
[Code]....
Have an objectdatasource that I need to iterate through and tally up values in certain catagories how would I do this.In short I need to grab each row check specific fields then increment counters I startted off below trying to use IEnumerable.is this the correct start?
View 4 Replieswhen I use Html.HiddenFor( model => model.OwnerId ) to create a hidden field, the value assigned to that field is zero. When I use <input type="hidden" value="<%: Model.OwnerId %>" /> to add the hidden field to the form, the value is assigned correctly.
Why would Html.HiddenFor( model => model.OwnerId ) not get the correct value from the Model object? Am I supposed to load model state somehow separate from returning the model object from the action method? Here is the view:
[Code]....
The Create action method is relatively straight forward.
public ActionResult Create( ... )
{
ViewStockItem item = new ViewStockItem();
item.ActionCode = ActionCode.Add;
if (item.OwnerId == 0)
item.OwnerId = 7;
BookOwner owner = db.BookOwners.Single(c => c.OwnerId == item.OwnerId);
item.OwnerName = owner.OwnerName;
return View(item);
}
i am just having a play with HtmlHelpers in MVC, very useful stuff and now i am trying to create one for dropdowns based on passing in a model ( any ) what the property is for the value and same for text.
so i have something like this:
[code]....
thats it
I have 2 different lists: EmployeeNames and Names. I read the values in Names and that of EmployeeNames.
If EmployeeNames exists in Names, I must not add that value to "ToSelectBox" but to "FromSelectBox".
If EmployeeNames doesn`t exist in Names, I must add that value to "ToSelectBox" but not to "FromSelectBox".
How can I do that dynamically?
I have 2 option values as follows:
<select id="fromSelectBox" multiple="multiple" >
<% foreach (var item in Model.EmployeeNames) { %>
<option value="<%=Html.Encode(Item.Text)%>"><%=Html.Encode(item.Text)%></option>
<%}
%>
</select>
select id="ToSelectBox" multiple="multiple" >
<% foreach (var item in Model.Names) { %>
<option value="<%=Html.Encode(Item.Text)%>"><%=Html.Encode(item.Text)%></option>
<%}
%>
</select>
how to open a subform model Form,Sub Window form using jquery that save the data ajex
View 4 RepliesI have a requirement in my application that, while saving the application, need to get all the value from drop down list and pass it to the view model. But application should not allow the user to select more than one item from drop down list manually, ie, only one value at a time. My view model is like ...
public class ListManagement
{
public IEnumerable<selectListItem> InactiveProduct { get; set; }
public string[] InactiveProductSelected { get; set; }
public IEnumerable<selectListItem> ActiveProduct { get; set; }
}
[code]...
Can i get a multiselect dropdownlist with option to select all and select none. I need it urgently....please send the control to arup.bhattacharya@[URL].
View 5 RepliesI am working in a school and we recently installed a new server running WinServer 2008R2. I want to be able to point people to a URL on our intranet and have them fill out a simple registration form and have this data written to a database. It would also be nice to have some data auto-populate (such as their name).
Is it overkill to set up a sharepoint server and try to do this with Access Webforms? Could I use something like dotnetnuke and find a module that works? Or how about options for writing custom forms?
I have my class RoomType:
Int32 Id
String Name
String ColorCode
My viewmodel gets a List<Roomtype> RoomTypes which should be displayed in a dropdown.
Each dropdown option item should have: 1) as title the Name, 2) as value the Id, and 3) the style background-color #ColorCode.
My problems are how to convert this list correctly into a List<SelectListItem> as required by ASP.NET MVC's DropDownFor helper, and then to have the correct values inserted for each option.
I've tried to have a new readonly property in my viewmodel, which has a getter RoomtypeSelectList which returns new SelectList(RoomTypeList) but I can't get the correct properties to show (Name, Id, Background color).
I am trying to understand a little more about implementing a custom model validator. I have implemented a custom model validator (derived from DataAnnotationsModelValidator<T>) for my custom validation attribute in order to do validation on the client side as well. There is not much documented about DataAnnotationsModelValidator. Besides, implementing client-side validation when is it a good idea or needed to implement a custom model validator for a custom attribute?
A separate question - in trying to understand how and when a custom model validator is used by the framework I noticed that my custom validator is always created with the context (2nd parameter in DataAnnotationsModelValidator<T>'s constructor) being a ViewContext, although it is declared as ControllerContext. Will a validator ever be created with something else than a ViewContext, and if so, when?
'm building a custom MetadataProvider and I'd like to access the actuall model value in the CreateMetadata method.
[Code]....
When the current model value is of type string, the model value can be found in metadata.Model. But when the model is of reference type the value is null.
So I have one custom model binder that inherits from DefaultModelBinder, where I am overriding the BindProperty() method to handle a type of field we've created. I also have one controller that we'd like to override BindModel() on, since we're handling an object in session for multiple views with that controller.
So I have CustomModelBinder : DefaultModelBinder, and then in the class where we override BindModel() i have that inheriting from CustomModelBinder. SpecialModelBinder: CustomModelBinder
But I have set a breakpoint in our override of BindProperty() in CustomModelBinder, and this never gets hit when using the controller that is also overriding BindModel(). Can I not inherit like this? What's happening here?
edit:
in global.asax:
ModelBinders.Binders.Add(typeof(ClassA), new SpecialModelBinder());
ModelBinders.Binders.Add(typeof(ClassB), new CustomModelBinder());
ModelBinders.Binders.Add(typeof(ClassC), new CustomModelBinder());
ModelBinders.Binders.Add(typeof(ClassD), new CustomModelBinder());
public class CustomModelBinder : DefaultModelBinder
{
// this will be hit in controllers that handle classes B, C, and D, but will not be hit in controller that handles ClassA
protected override void BindProperty(...){}
}
public class SpecialModelBinder : CustomModelBinder
{
// this will be hit when working in controller that handles ClassA only
public override object BindModel(...){}
}