C# - Does MVC 2.0 Model Binding Work With Ajax Requests
Feb 23, 2010
i'm just sending a normal POST request using Ajax.BeginForm... i output the form elements using the .TextBoxFor and .HiddenFor etc... all as i should... and when it's posted via ajax to my action method, the object in the action method (named "Comment") is not populated with the values!
Am i missing something? here is the relevant part of my code to those who want to see it...
[code]....
View 2 Replies
Similar Messages:
Jan 5, 2011
I'm having problems retrieving the values of a selectlist in my form collection. I've tried making a viewmodel with an attribute with the same name as the select list.
I'm honestly just realizing I REALLY don't understand how model binding works with selectlists. I've just been assuming that the following conventions apply:
Name the select list the same thing as the attribute on the model you want it to bind to. Apart from that, I really don't get it. I've looked at several books on it and they're useless frankly. How does a select list work with a) form collection and b) a particular model?
View 1 Replies
Feb 21, 2011
I'm am experiencing some strange behavior with a website. I have a page that performs a simple AJAX request to fill a div. The data is fetched from a page in the same domain. When I perform the request using [URL]. The requests work perfectly, but when using [URL] or [URL]. My requests are simply ignored. I've used Firebug to track requests, and not even a single ajax request is performed. Normal requests (such as fetching the main page that invokes the ajax code) works fine on both scenarios.
View 2 Replies
Jan 3, 2011
I'm building a web application that has a particular model representing some events. Users need to be able to add N number of people to a given event. Choosing people is handled by a partial view.
I'm trying to build a menu that displays when users click "add a person" to the event. Because the event hasn't been filled out completely yet, there is nothing in the database to persist between requests.
I also have validation logic on the event page.
My proposed solution is to add the form to search or add for people on the event form itself and have a submit button that sends the values that have been added back to the server, where I can store them in ViewData or Session.
Unfortunately, doing this flags the validation.
My second solution is to load a partial view responsible for loading the UI to add/search for a person. I could add a little code on the method in the controller that returns a partial view storing the existing data in a session variable or viewdata. Trouble is, I have to submit the form to do it--again tripping the validation!!!
I'm wondering if perhaps I chose the wrong tool to do this...because in webforms, there would probably be a postback and you would just perform an operation on that postback. I'd like to avoid rewriting the application in webforms and am wondering if there are ways I'm overlooking in ASP.NET MVC.
View 2 Replies
Jan 3, 2011
I'm building a web application that has a particular model representing some events. Users need to be able to add N number of people to a given event. Choosing people is handled by a partial view.
I'm trying to build a menu that displays when users click "add a person" to the event. Because the event hasn't been filled out completely yet, there is nothing in the database to persist between requests.
I also have validation logic on the event page.
My proposed solution is to add the form to search or add for people on the event form itself and have a submit button that sends the values that have been added back to the server, where I can store them in ViewData or Session.
Unfortunately, doing this flags the validation.
My second solution is to load a partial view responsible for loading the UI to add/search for a person. I could add a little code on the method in the controller that returns a partial view storing the existing data in a session variable or viewdata. Trouble is, I have to submit the form to do it--again tripping the validation!
I'm wondering if perhaps I chose the wrong tool to do this...because in webforms, there would probably be a postback and you would just perform an operation on that postback. I'd like to avoid rewriting the application in webforms and am wondering if there are ways I'm overlooking in ASP.NET MVC.
View 1 Replies
Sep 16, 2010
I've just discovered that the X-Requested-With header in IE is always "XMLHttpRequest, XMLHttpRequest" instead of just "XMLHttpRequest". I found the reason in MicrosoftMvcAjax.debug.js at line 280 (MVC 2.0). Seems there is no need to add this header again as it is already done in Microsoft AJAX Library. And because of this IsAjaxRequest extension method doesn't work anymore if there is no X-Requested-With param in the request body. And this make X-Requested-With header useless in IE.
In Firefox there is no problem as it doesn't append but replace the existing header value. Not tested in other browsers.
View 9 Replies
Mar 31, 2011
I have a piece of code written in 2 different ways, one a web service and another a WCF client which are trying to access an external 3rd party URL.
The same code when written as an exe works fine, i see my submissions on the 3rd party site, while the web code does not work.
I installed Fiddler to check traffic and to my surprise the web code works when Fiddler is capturing traffic, As soon as I stop Fiddler my code does not work.
For the web service code I get error: The remote name could not be resolved:
For the WCF client I get error: There was no endpoint listening at <<3rd party URL>> that could accept the message. This is often caused by an incorrect address or SOAP action.
Am quite sure now that this is because of some network and/or proxy on my corporate network, but not able to figure out what it can be.
View 1 Replies
Jun 14, 2010
This seems like the model binding is causing me issues.Essentially I have a model called ProductOption and for the purpose of this question it has 2 fields
ID (Int) PK
ProductID (Int) FK
I have a standard route set-up
context.MapRoute(
"Product_default",
"Product/{controller}/{action}/{id}",
[code]...
This is used to determine (on submit) if we are editing or adding a new option. However the Model binder in .net seems to replace .ID (Which was 0 when leaving the above get actionresult) with 1 (or the value of the id parameter in the URL)How can I stop or work around this?
ViewModel
public class ProductExtraModel
{
//Database
public int ID { get; set; }
public string Name { get; set; }
public int ProductID { get; set; }
public ProductModel Product { get; set; }
}
View 2 Replies
Dec 14, 2010
Have been able to get a really strange behavior in mvc 3 rc 2, where an controller action (a) in an area has a nullable enum and long as parameter and an controller action (b) (which is not placed in an area) only have nullable long.
If I compile and first surf to (a) then it works, and then surf to (b) with a number as parameter it throws exception where it seem to have tried to pass the nullable enum!
If I recompile and surf to (b) with same parameter it works, and if I surf to (a) the enum is always null.
Edit: perhaps this is a model binding bug, perhaps caused by some internal cache? Since the routes are correct but the passed arguments are not.
View 4 Replies
Apr 15, 2010
I have just upgraded to MVC 2.0 and I have noticed a difference in the model binding when posting a form back to an action. In MVC 1.0 if I had a posted a blank form back to my action and then looked at the populated model class my empty string values would show as empty strings, in MVC 2.0 doing the same thing and then looking at my model class shows my empty string values as null, why has this changed?
View 2 Replies
Feb 6, 2010
I am experimenting with using xml as a database for small CMS, like gallery or staff profiles etc
how i bind my xml document to a modelclass so that i can then use that class for strongly typed views:
here is my model class:
[XmlRoot("employee")]
public class EmployeesModel
{
[Required]
[DisplayName("Name: ")]
[Code]....
View 1 Replies
May 6, 2010
I've got a very complex form and i'm using the MVC model binding to capture all the information
I've got it set up to capture all the different submissions that can happen as there are about 10 different submit buttons on the form, and there are also 2 image buttons
I tried to get a bit clever (or so i thought) with capturing the image button submissions, and have created a child class so that i can capture the x value that's returned
[code]....
The html for the image button then looks like
<input type="image" id="ViewDataObject_ImageButton" name="ViewDataObject.ImageButton" />
This works fine in all browsers except for Chrome.
When i debug it in chrome, the Request.Form object contains the values that i would expect, but after the model binding has occurred, the ImageButton property on the ViewDataObject has been set to null
The only difference that i can see between the submission values is that Chrome passes the x as lower case (ViewDataObject.ImageButton.x) and IE passes it as upper case (ViewDataObject.ImageButton.X) but i didn't think that model binding took any notice of casing on property names.
View 1 Replies
Apr 29, 2010
I am trying set up a simple dropdown list but I dont seem to be able to get it to bind to the Model. I am using Asp.Net MVC and nhibernate.
My dropdown list is declared like so:
<%= Html.DropDownListFor(model => model.Project, (IEnumerable<SelectListItem>)ViewData["Projects"], " -- Select -- ", new { name = "Project" })%>
I set up the select list like so:
ViewData["Projects"] = new SelectList(projectRepository.GetAll(), "EntityGUID", "Name", editEntity.Project);
This seems to bind the select list to the Dropdown fine, but the SelectedValue is not set.
it shows up as the default --- Select ---
Also when I save this data, the dropdown does not bind to the model, I have to manually set the object like so to save it:
entity.Project = projectRepository.GetById(new Guid(Request["Project"].ToString()));
I believe I have take the correct messures to have this item bind directly to my model. Is there something I am missing here?
View 2 Replies
Feb 3, 2010
I am working on the example from Steven Sanderson's book 'Pro ASP.NET MVC Framework'. The cart class is coming up as empty in my Index action method of CartController class.
[Code]....
View 2 Replies
Apr 16, 2010
I have a textbox that I am defining as
<%= Html.TextBox("Username", Model.Form.Username,
new { @class = "textbox", @disabled = "disabled" })%>
In my action
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult EditLogin(LoginForm post) {
return View(model);
}
post.Username will be blank, all other properties bind correctly, but if I change @disabled="disabled to @readonly="readonly" the username binds properly and everything works. It looks like model binding ignores values in disabled fields. Is their a way around this? I still need the field's value to bind to the model. I can use readonly but would prefer to use disabled so it is visually apparent to the user that they cannot edit the value of the field.
View 3 Replies
Nov 24, 2010
I would like to create a custom action filter attribute that adds a value in the HttpContext items that would be accessible during model binding.
I have tried to add it in the OnActionExecuting but it that seems the modelbinding is exectued before the filter. Maybe there's a method in the modelbinder that I could override that will be fired after the filter and use the value injected by my filter.
What I want to do, is to inject a validation context (the library I use for validation supports context, it is nvalid.net (www.nvalid.net) I would like to be able to place an attribute such as
[ValidationContext("Prevalidation")]
on my actionresult method, so that the validation that occurs in my custom model binder could know which context to use when doing the Validation. That's why I can't simply make a custom model binder.
View 2 Replies
Mar 2, 2010
I am working with ASP. net MVC project. There is File upload functionality to be implemented in the project. I am able to show the upload control using input-file. But I am not able to show defalut value (file with path) in text area using model binding.
Can we bind model to file upload control in MVC so that we can show Previous file (already uploaded) in the text area.
what I have in view is this
<input type="file" name="fileupload1" id="fileupload1" />
can we write something like this
<input type="file" name="fileupload1" id="fileupload1" value=Model.Object.FileName/>
will it show the previous file name and will the model get bind?
View 3 Replies
Nov 2, 2010
I would like to declare some properties of my View Model of type TimeSpan to display the TotalMinutes property and bind back to a TimeSpan.
I've bound the property without using the strongly-typed helper in order to retrieve the TotalMinutes property:
<%=Html.TextBox("Interval", Model.Interval.TotalMinutes)%>
When the field is bound back to the View Model class it parses the number as a day (1440 mins).
How can I override this behaviour on certain properties (preferably using attributes on the View Model itself)?
View 1 Replies
Mar 12, 2010
I have several models that implement a array property using null object pattern in the following manner:
[Code]....
The problem is that any time I attempt to bind to a property of this type the DefaultModelBinder.CollectionHelpers.ReplaceCollectionImpl method wants to call ICollection<T>.Clear() and then ICollection<T>.Add() on the zero length array.
The real issue seems to be in DefaultModelBinder.BindComplexModel() method however. The method contains an explicit check for arrays and special handling to account for the fact that they don't support clear/add functionality. The problem is that the check stipulates that the array model must also be null. If it is non-null, normal ICollection<T> handling ensues which presumes support for clear and add. Can any one explain why the null check is there? It seems to me that either 1) the null check should be removed to permit replacement based binding to non-null arrays or 2) an explicit error stating that non-null arrays are not supported should be thrown.
I vote for the former. I can understand that in general, replacing an existing model with a refrence to a different model would be considered a bad thing. The calling code would not expect this in general and it could lead to hard to find bugs. However, in the case of arrays, if the calling code expects the array property to be modified at all, then I think the normal expectation would be that such modification would occur via replacement of the array.
View 1 Replies
Nov 10, 2010
I incorporated "JQuery UI DatePicker" from this site [URL] to my web application. I have it working but my issue comes with incorporating it with my strongly typed views.
I have included my revelant code below. The view is bind to the "NewEditDataTypeModel" model. I have 2 questions:
1. I have a date variable in my model that I pass to the view, how do I setup the DatePicker to use data from my model has a default value?
2. When an user selects a date from the DatePicker, how do I bind it to the model? Currently this value is passed in as a separate DateTime parameter in the action method (showned below).
I have a HTMLHelper Extension for the DatePicker:
[Code]....
Inside my view it looks like this: (code snippet)
[Code]....
My action method in my controller:
[Code]....
View 9 Replies
Jul 30, 2010
how controls (txtbox, radio, etc) are mapped to properties in my Model. Now to the next part : My scenario (example):
Model: three tables: tblPerson, tblPersonOptions, tblOptionsConfig
UI:
Person Name: ______ (required)
Person Age: ________ (required)
radio button: Does Person want to select options? Yes / No ? (required) If yes, then show a checkbox list of options from tblOptionsConfig: (required if yes above). Scenario I tested:
Person Name: Tester User Person Age: Left blank (check if validation error is thrown)
Radion button: Yes. in options check ..let's say 3 out of 5 options. The UpdateModel throws the validation error and the page is returned back to the view(add.aspx).....................
View 9 Replies
Sep 17, 2010
i am working in creating forms based wizard application.user interface is something similar of using ASP.Net wizard control. i like to create 5,6 pages with data collection forms (each page with each section) and final confirmation page. can any one tell me better way of building it using MVC. I am not sure how to maintain state across pages ( Model binding?) .
View 2 Replies
Nov 17, 2010
As far as I know there is no way to get the ValidateAntiForgeryTokenAttribute to work with a controller action that is using model binding to JSON. I tried adding the __RequestVerificationToken property to the JSON object before posting but it didn't pass the verification.
View 14 Replies
May 18, 2010
I have a question about model binding. Imagine you have an existing database entity displayed in a form and you'd like to edit some details, some properties eg createddate etc are not bound to the form, during model binding, these properties are not assigned to the model as they are not on the http post data or querystrong etc, hence their properties are null. In my controller method for update , Id just like to do
public ActionResult Update( Entity ent)
{
//Save changes to db
}
but as some properties are null in ent, they override the existing database fields which are not part of the form post data, What is the correct way to handle this? Ive tried hidden fields to hold the data, but model binding does not seem to assign hidden fields to the model.
View 1 Replies
Nov 7, 2010
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]....
View 2 Replies