MVC C# Submit hidden form using jquery - c#

Multiple forms submit with C# MVC.
The company I work for has taken the approach of having multiple forms submits being done using jquery submitting different forms on different button clicks. My concept should be pretty simple. I have a text box sitting in a form that holds the quantity of items to purchase. I have two separate buttons, one for adding to cart and one for adding to wish list.
The jquery submits the form, but the formcollection in the MVC code does not have any form data.
Is My jquery not submitting the form? Or not submitting the hidden fields?

if you are sure form is submitted then i suppose there is some problem in the view. all inputs should be submitted for sure, even if they are not visible (e.g. style="display:none;"). i could be more specific if i would see the view.

Check the Action signature in the Controller and be sure that you're using the right parameters' name.
Just for curiosity, why you're not using ajax for this ? (#Html.BeginAjaxForm () )

Related

Multiple entries in asp.net form with one submission

I am creating a form which will be used to nominate multiple people from a various teams for an award. There is no limit on the amount of people that could be nominated so it is hard to say how many nominee info I should add to the form. I am curious if there is a way to create a nominee info i.e. Full Name , Email , etc with an option too "add another nominee" where the user could specify more than one person info on the form without submitting multiple forms. Also user should be able to edit or view the already added nominee info's
Example:
About your nominee:
Nominee's Name: Steve
Nominee's email : steve#gmail.com Nominee's
Country : drop down list
add another nominee
You could create a form (or a modal popup) with a Html Table, an Add button and a Submit Button.
The Html Table is used to store the nominee information.
The Add button is used to dynamically add rows in the Html Table, inorder to add new nominee information.
The Submit button is used to submit the nominees. Use JQuery to loop through the table and post the list of entities to the action method.
More detail information, please check the following links:
Dynamically add rows to HTML Table in ASP.Net MVC
Pass (Send) List of objects from View to Controller using jQuery AJAX in ASP.Net MVC

Required DataAnnotations needs BeginForm to fire validation in MVC5

Is it necessary to write and place all controls inside
#using (Html.BeginForm())
{
// HTML Elements and HTML Helpers.
}
while using [Required] DataAnnotations ?
I am facing strange issue in MVC5 based application. The problem is that I have used one property named e.g "Credit" in model and the datatype of this property is integer and set[Required] DataAnnotations above that property.
But I haven't used Begin form. So in this case validation doesn't fire. whereas If I write BeginForm then validation works.
So, Is it necessary to place all html elements & html helpers inside BeginForm to validate controls ?
Thanks
-Nimesh.
If you want the client-side validation to work, then yes the form controls etc. need to be within a <form> tag (as generated by the HTML.BeginForm helper). Server-side validation would still work regardless of this.
Like the commenter above, I would question why you want to have controls outside a form tag in the first place. Even if you plan to submit the data back using Ajax, it's better semantic design to use a form tag, because it's clear which data items belong together, and it also makes it much easier to gather the data to submit via ajax (e.g. if you have jQuery, you can use $("#myForm").serialize() to automatically collect the values from all the controls within a form and pass that to the ajax request).
We need to validate something when we post some data to the server, right. And for posting some data to the server you will need form tag, whether you use BeginForm() or the <form> tag. You need tags inside the form those will be validated by the server.
I guess, this will give the answer to your question. Enjoy!

how to get the ViewModel from the View to the Controller without submitting the form?

I have an ASP.NET MVC5 app using Razor.
One of the view displays a form.
I would like one of this form's button to call the controller via ajax and pass him the current ViewModel without submitting the form.
Not sure if I am clear.
I know how to get the ViewModel back from the View to the Controller when submitting the form.
But in this case, I don t want to submit the form, but I need to pass the ViewModel.
Is this possible?
I suppose I could pass to Ajax the form serialization via Jquery this way:
$("Formid").serialize()
But maybe Razor is able to pass the ViewModel directly?
Thx in advance

Linking two forms in ASP.NET

I have an HTML form that handles some basic information. When the button for completing the form is clicked, I have a popup asking for additional information that appears (this is a div that is hidden until button click event). What I want to know is if there is a way to link those two forms together so that a final submit will POST all data on both forms?
Ideas I've Considered:
On button click, pass the 11 fields from form one into 11 hidden fields from form 2.
On final submit, create my own post call via javascript.
Both of these would work, but seem sort of hacky to me. I'd rather have a true method of linking two form submits if there is one. Anyone?
Why not just make the first button a NON ASP.NET button (i.e. just a regular HTML button)... with an onclick event which shows the hidden div. The Submit button on the hidden div would be the ASP.NET button which submits the entire form (data from both parts) to your ASPX?

Filtering Pane, ASP MVC 3

My plan is to create a a two-pane page using ASP MVC 3. The left pane should be a small filter pane and the right the main content, showing a list of objects (say products).
Initially all products will be shown since no filter is applied. When selecting "only red", only red products are shown in the list. When further selecting a price range, only products in that price range will be shown.
Functionally the plan is to implement the filtering pane as a treeview with checkboxes (to be able to drill down to more and more specific filtering options), graphically it will probably be enhanced in some way to improve usability.
What is the best way to implement the coupling between the filter pane and the main list? Everything should work server side, but should of course use javascript (jQuery) when possible for direct feedback.
The simplest way is probably to make it closely coupled solution, calling a specific Asp MVC action using a custom-built javascript (with fallback to a form post). Doable enough, sure, but how to make the solution reusable? Also it would be nice to not loose all filtering data when navigating forward and back, i suppose GET arguments is the only decent way to do that?
Are there any best practices, any guidelines or anything to base this on to make a nice modular structure for filtering.
Victor, I recently had to solved this same problem. I'm not promising it's the best way but it's pretty clear and should even work well in case JavaScript is disabled (who even does that anymore?).
Create a that calls the action with all the field-selectable search options like "only red".
To that same form, add empty, hidden value for the things not directly as fields (paging, sorting...)
Setup your form with the excellent and very easy to use JQuery.Forms (http://www.malsup.com/jquery/form/) to make you form submit via JQuery (all your form values will be passed as JSON on form submit).
Make your back/next/paging/sorting links pass their individual values via query (no-JS fallback) and use JQuery to capture their click events. The JQuery click events on those links should assign the value of the value passed by the link (page number, sort column name...) to the corresponding hidden field in the form and call submit (with thanks to Jquery.Forms will submit via AJAX).
When you configure JQuery.Forms, you can define a callback method. In the callback method take the result (the HTML returned by your action that should contained your filtered+sorted+paged result) and replace the document (or DIV if you used a partial action + view) with that.
The result is a JQuery solution that works when JS is off. You also have very minimal JS code to write other than wiring the event handlers.
I'm not sure if it will be helpful but in MVC 3 you have access to a property called IsAjax from the view so you can do specific things to server a slightly different view when called from AJAX.
Cheers

Categories