MVC3 Restricting validation to the submitted form only - asp.net-mvc-3

I have a view with two forms on it. Each form is marked like this:
Html.BeginForm("Details", "Forum", new { page = Model.PagedList.CurrentPage }, FormMethod.Post)
And each form has its own input button (type="button").
My problem is, when I click the button for one of the forms, the validation errors for the other form are added to the ModelState, so ModelState.IsValid == false.
How can I limit the scope of the validation to just the form I am clicking a button on?

Use Shared View instead to control your validations on different form.

Related

How to prevent validation from non-selected rows in MVC3

I have a requirement in MVC3. I have a simple form with a gridview with edit, update and delete button.
When I update any record, validation should be fire for only that single row, not for other rows.
Upon clicking edit load a partial view into an Ajax dialog. Validation won't work on that loaded partial view, you'll have to tell jquery validate to parse that new content to validate it ala MVC 3 Razor. Partial View validation is not working

Partial View Submit inside Parent View MVC 3 w/Razor

I have three partial views that I load into a div based on radio button selection using jquery. Everything works fine on the loading and on the forms, however each partial form has a submit button(none on the parent form). I begin each partial form with:
<div id="newTicketPartial">
#using (Ajax.BeginForm("CreateNewTicket", "SubmitTicket", new AjaxOptions() { UpdateTargetId = "newTicketPartial", HttpMethod = "POST" }))
The issue is that within this form once I finish entering data and click the submit button it seems to skip this forms ajax post back header and instead uses the parent views #using Html.BeginForm
If I put an action and controller in the parent forms BeginForm field it will use that, otherwise there is an error. My problem is even if it does use the parent views POST path my controller only returns the partial view that I was editing, which updates the entire page. I need to be able to submit information from each partial view and have only that partial view be updated, not the entire page. I am open to suggestions of ways this can be done. If more code is desired I will post more.
Nested forms are the issue. You should not nest forms, or else any buttons further down the page (even those generated by partial views) will cause the parent form to POST.

Selected Dropdown value changes when validation fails-ASP.NET MVC

I am using MVC 3 with ASP.NET. I have a dropdown box and getting it populated from database. I am using validation on the View. If it fails the validation, I am displaying the same view with errors being caught in ViewDate.ModelState.AddModelError.
I am checking for the ViewData.Modelstate.IsValid property if true then execute the code else display the errors in the view.
It is diplaying the errors in the page, but the selected value in the drop down is getting reset when validation fails.
How do I make sure the seleceted drop down does not change when validation fails?
In the action that handles the form submission and validation, make sure you set the properties on your model object from the form before rendering the form view.
For example, in this question you can see how the Dinner object parameter in the Create action is reused when the View() is returned.
Set a breakpoint in the action handling the submission and check the property for the list of values. If it's null or empty reload it.
If your dropdownlist is being populated via javascript then it is possible that the property holding the list of values is empty on submission. This is common when using cascading dropdownlists such is the case with loading Province / State lists based off country. All cascading lists loaded after the model has been passed to the view must be reloaded using the selected value of for each dropdownlist in the controller action that is handling the submission.

JQuery $.post() submitting multiple forms instead of just one

I have a page with several forms on one page. I have a button (outside all forms) that I want to submit only ONE form, but all form data is getting submitted from all forms.
$("#indiv_save").click( function() {
alert($("#indiv_form").serialize()); // Shows only correct form
$.post("/admin/update", $("#indiv_form").serialize(), function(data) {
notify(data); // quick and easy feedback
});
});
The alert() call shows exactly what I want to submit, but then Firebug shows fields being submitted from all forms after the .post(), even though the serialize() function is identical.
All forms have different names and element ids.
What am I doing wrong?
You are making an ajax POST and returning. Depending on your button, it's going to attempt to submit the page's forms. What you need to do is prevent the default action:
$('#indiv_save').click(function(e)
{
e.preventDefault();
// Your Code Here
});
The default action for your button is probably "submit the form", and you are not preventing it.
Return false from your click function should do it.
The forms can't be inside a single all encompassing <form> tag, they have to be separated out in individual <form>'s instead.

passing checkbox and textbox values from a view form to a controller action

I have a view with a form..this form has a textbox and a checkbox in it.
i also have a submit button in the form which points to an action in a controller.
my question is..how can i pass the values in the textboxes and the checked state of the checkboxes to the controller action?
the textboxes and checkboxes are not tied to a model.
thanks
You have two options.
The best option would be to create a ViewModel. This model doesn't need to have any logic or anything behind it, just a public get/set for the properties. This means you can also use the MVC validation, helpers and binding on the values and makes it easier to work with.
The alternative is for your action to accept a FormCollection, this is basically a dictionary with the form values, the keys being the name attribute on your form elements.

Resources