Monday, April 8, 2013

Passing extra parameter to controller in jquery.form.js


In MVC, when we have to submit the page via ajax, lots of us uses "jquery.form.js" to upload file. And sometimes it is needed to pass extra parameters along with selected file. I had such requirements and found that, if the controller has some parameter that accepts for example Firstame, LastName as extra parameter, we have to create inputs (hidden or visible) with exact same name and when the form is submitted, it will automatically assigns the value from these input to the parameter.

It is described as below.

Controller

public string AjaxUpload(HttpPostedFileBase file, SOMECLASS obj)
{}



Here SOMECLASS has members like

public class SOMECLASS {
           public string FirstName{ get; set; }
           public string LastName{ get; set; }
    }



Most of the times, we try to pass FirstName and LastName as parameter as a part of "data" field while using ajax. But this wont work in case of jquery.form.js



But if we have following field in form



input type="text" name="FirstName" id="FirstName"
input type="text" name="LastName" id="LastName"



second parameter of AjaxUpload "Obj" will have values directly from Firstname and LastName inputs, when the action "AjaxUpload" is called.




No comments:

Post a Comment