Friday, June 29, 2012

How to pass a model from view to controller using javascript in MVC 3

JS Code:



Funtion PassModel(){

var UserModel = {
                UserName: ‘Test’,
                PhoneNo: ‘00000000000’,
                Address: ‘ABCDEFGHI…’
};

var URL = "/UserController/UserAction?userdetails=" + UserModel;
                    $.get(URL, function (data) {
                        ShowResult(data.SaveResult);
                    });

}


HTML Code (View):



<input name="ClickedButton" type="button" id="btnSaveUser” value="Save New User"
                    onclick=" PassModel ()" class="commonbtn FR" />



C# Code (Controller):



public ActionResult UserAction (UserModel userdetails)
        {
            int SaveResult = DAL.SaveNewUser();
            return Json(new { SaveResult }, JsonRequestBehavior.AllowGet);
        }

No comments:

Post a Comment