I've got a DataForm in which I replace some TextBox fields with DropBoxes to imitate a lookup using the AutoGeneratingField event.
However, when I do that, I loose any data annotations that could otherwise be used (such as a description which I specified in my data model).
Is there a way to get these values from the data model without resorting to auto-generated fields? The DataFormAutoGeneratingFieldEventArgs parameter doesn't seem to contain them.
Set the DataField.PropertyPath property to the name of the model property with metadata.
Related
I'm using the Google ReCaptcha element on a form. ReCaptcha adds a parameter to the POSTed parameters named g-recaptcha-response, which the controller needs to retrieve. Since this parameter name is not a legal C# variable name, it seems that the only way to retrieve its value is via Request.Params["g-recaptcha-response"] (rather than via model binding to a property in the view model).
Now, my problem is that elsewhere in the POSTed parameters I have a couple of form fields that may contain HTML markup. I have annotated the associated properties in my view model with [AllowHtml], which prevents the model binder from throwing an HttpRequestValidationException if the user enters HTML markup into the form. But [AllowHtml] apparently only works in the model binder. If the user has entered HTML markup into the form, then I get an HttpRequestValidationException when I reference Request.Params to fetch the ReCaptcha response.
As near as I can tell, the only way I can fetch the ReCaptcha response while still allowing HTML markup in selected POST parameters is to use go through the pain of writing a custom model binder (e.g. https://stackoverflow.com/a/4316327/1637105) to allow me to bind a property in my view model to an alias (in my case, a property name that is not a valid C# variable name).
The point of this question is just to confirm that I really do need to go to the pain of implementing a custom model binder.
Any suggestions or alternate solutions are more than welcome!
EDIT:
It occurs to me that another solution would be to figure out a way to fetch the value from the POSTed parameters without triggering (or while handling) the HttpRequestValidationException.
You can use the Unvalidated property of Request to access values without triggering Request Validation. for example
var captcha = Request.Unvalidated.Form["g-recaptcha-response"];
The first part of my web app was creating an interface where the user can create field names and the data type to associate with the field names.
Now I need to create the 2nd part which is pulling those field names from the database along with the data types associated with them and creating html controls on the fly using those fields names/data types. For example, if one of the field names is "birthdate" and the data type is "datetime", then my view should automatically create a html textbox with name attribute "birthdate". If a field name is "Active" and the data type is a boolean, the view should automatically create a checkbox with name attribute "Active". Anyone have any suggestions on how to do this in MVC? I'm assuming I need to use reflection and that when I have to post back to data, I will have to send back a json object.
you can use mvc htmlhelpers. for example a flexible htmlhelper for dropdownlist that accept collection and other parameters. another htmlhelper to get collection and delivers listbox and .....
and in your view you can use of this htmlhelpers by their types.
I didn't create my own htmlhelpers, but I did something similar. I used string builder to dynamically create the html in the get method of my controller and then posted that to the view.
Code for binding source:
Component.DataSource = new BindingSource(yourSource, ????);
All the examples that I saw follow this patter, and instead of ????, I always see a null.
The Docs say the parameters are data source and data member, but I don't understand what is the purpose of the data member or how to use it.
The DataMember parameter is used when the source contains multiple columns, lists, or tables of data and you need to specify which one is being used. From the MSDN for the DataMember Property:
If the DataSource contains multiple lists (or tables) of data, you
should set the DataMember property to the name of one of the sources.
The constructor that you are asking about simply allows you to specify this property at instantiation.
I am attempting to put auditing in my application based on properties that have changed. I was attempting to follow this example however the Entity Framework only keeps track of scalar and complex type properties, not navigation properties as described here.
So in a different approach, I figured if I could find which FormCollection values had been changed then I could use those. Is there anyway to easily determine if a value has changed between being loaded in a form and being submitted?
I guess you want to check this in the controller.
The easiest way should be to reload the data (the same you did to send the data to the view) and compare this with the values you got. Aside from this you could use a lot of Hidden-Fields in your form and check those (initialise to the same values as your inputs) - but I don't really like this.
I am using ASP.NET MVC 3 and have a need to detect if the form field has been changed on the server side. I know about using tricks with hidden fields, but I was wondering if there is a way to do it by just using the API?
Basically, I have edit screen for my model and one of the fields is an optional id that can be specified. If the field is specified, I have to insure it is unique (no other model has it). So on the edit controller, I want to run the validation but only if that field has been changed.
Please note, I don't need to know previous value vs. new value, just if the field value has changed.
You will have to keep a copy of the old value somewhere, and do the comparison. You may store it in your View Model.
There is indeed no 'dirty' flag - MVC actually is closer to "the way the web works" to reuse that statement. All that is sent over are name value pairs. nothing else. MVC's model binder just matches those names to your object - so in order to truly detect a change you have to either validate against the true data source upon post or compare values passed in on the form - in which case - it is best to hash to avoid forgery.