Disable form validation in MVC2 - asp.net-mvc-2

Is it possible to programatically disable form validation in MVC2?
I'm hoping there is some kind of command that I can put in a custom ActionMethodSelectorAttribute, making it possible to prefix ActionResults where I want form validation disabled.
The reason for this is that I have a form with multiple buttons, one button adds a new row to a child object of the model - which consequently creates a new row on the form.
This works fine, but validation is fired for the form each time "add" is pressed, and so the new fields flag up validation errors before the user has a chance to enter data.

I've found an alternative solution to this.
Rather than use multiple form buttons, I've used a custom html helper which produces a "Add" link.
This helper places the current Model in TempData and produces a Link to Add/{id}.
I'm not sure this is the cleanest solution, but it allows child elements to be added without validation firing. To tidy things up a bit, I specified the form as such:
Html.BeginForm(view, controller, new { id = Model.ID })

Related

wicket - two submit buttons with different validation

I am using two AjaxButton for the same grid where both of them are not in the form but attached to it.
activateButton = new AjaxButton(ACTIVATE_BUTTON, grid.getForm())
blockButton = new AjaxButton(BLOCK_BUTTON, grid.getForm())
my problem is trying to give each one of them different validation, when the form is submitted i am getting all the buttons validation instead of getting just one of them as I want.
For each of the buttons you can disable the default form processing using AjaxButton.setDefaultFormProcessing() and handle the validation directly in the button by overriding AjaxButton.onSubmit().
I would turn off default form processing and control validation manually.
See wicket manual

windows form validation issues (.net2.0 )

I need help with some things regarding windows form validation:
closing form using standard form closing button (X) triggers validation of all controls.
Is there a way to supress it when closing form using this button, and force it only when pressing my own button?
validation of textbox (possibly other controls, I tested only textboxes) wont invoke when i change text (value) programatically. I need to type text directly into textbox if I want validation to be triggered later, before form is closed. How to tell the form that some control needs validation (but not to trigger it immedidately)?
Thanks.
EDIT:
(1) solved, using this answer.
(2) now, after i set AutoValidate property to false and added ValidateChildren() to my button, only 1 control is being validated with its current value, values of all other controls are revert to value binded to them from DataSource object . I checked it in Validating event - only first control validating keeps its current value, after this validation is finished, other controls' values are replaced with values from DataSource object. I don't understand why. Any clues?
Try this, maybe it could help you. ( for 1)
In the Forms Load event you can put
this.ControlBox = false;. This will hide your X button with the other buttons at the top.
The Form has a Form1_FormClosing event. In that Event you could call the triggers you need. Put a button on the form, and in button_Click event you type this.Close().

How to post partial view to another controller method

I have a one form tag inside my Index.aspx view. Index.aspx contains several partial views and using the same model to render them.
Now when any partial view is posting the form with submit button form is posted to OneActionMethod. But I want for some partial views to post form to OtherActionMethod.
How can I achieve this, without using action links, just with submit button in this particular patial view?
I`ve wrote the update in comments to this question. Answer is still not clear to me.
i believe a little javascript will get ur job done. u have to hook the submit event of the form and change the action attribute of the form. remember action is attribute of form not of a submit button. in jquery u can do something like
$("#myform").submit(function(){
if(isFirstSubmitButton){
$(this).attr(FirstAction);
}
else if(isSecondSubmitButton)
{
$(this).attr(SecondAction);
}
return true;
});
You sound like you are trying to program "WebForms" style in MVC.
Why do you have one big form enclosing all of your partials? Separate them into unique forms, and have each one post to it's appropriate action.
EDIT: With your further clarification, the only thing I can think of (aside from redesigning to use individual forms, which does lead to problems if they want to share data), is to post to a single action, and then route the request to a private member within the controller for ActionA or ActionB depending on a particular form element.

Zend Form multiselect array

I am using the Zend Framework and have setup a normal Zend Form, what I want to try to achieve is to have a button (with some javascript) that says add more and it adds another drop down menu same as the one setup in the zend form (a clone of it).
basically when the button is clicked it adds another select box like so:
<select name="type[]"> ...</select>
I can do a copy of the multi select box with a different name and insert it in the DOM and catch the post from the controller outside of the Zend form but what I was wondering if there is a proper way to achieve this and be able to validate and populate the extra fields when editing a current data stored in db if there are any extra.. Any help is appreciated, thanks.
Well remember that in your controller if you have something like:
$this->form = new Form_Someform();
You can always do:
$this->form->addElement(etc...)
Right before using isValid() or populate.
So in your controller when someone submits the form, when creating your form object you could check if any select were created dynamicaly and then create corresponding Zend_Elements and just validate against that.
Also when you reload that form you just create elements depending on whats in your database.
You could also use the forms constructor to pass in an array of selectboxes and create then right there too. Thats what I do.
The important things to remember is that you have control on the constructor and on the form object between its creation and the use of the populate() and isValid() functions.
Hope this helps.

MVC submit form to different controllers

Is it possible to post the same form to different controllers?
Each page could be post only to form action url, but may be some how i may say to button to which url form should go?
e.g i have one form and two submit buttons, one button will post form to one controller/url (eg /action/view) anther button submit form to one to another controller/url (eg /action/anothervew).
You can definitely do this, use JQuery (or just javascript) to attach a function to the onclick event of the button(s). Then use that function to change the URL that the form posts to and then submit the form.
JQuery would be something like:
$('#button1').onclick(function(){ $(this).action = url1; $(document).submit();});
$('#button2').onclick(function(){ $(this).action = url2; $(document).submit();});
You will need to use javascript for this. When the button is clicked have the javascript modify the form's action property to the appropriate controller and then submit the form.
We've done this before using javascript, as mentioned in other answers, and that's probably the correct way to go. An alternative, however, is to post to a single controller method which contains logic to decide where to send the form data off to.
Effectively, you submit the form to the controller, and the resubmit that data based on the text or id of the button clicked using an if statement in the body of the controller action.