0

I'm new to javascript and MVC I'm working on a sample application containing a sign up page and I'm using ajax to done the process my current code is given below

  function create() {
        var user_name = $("#txtUser").val();
        var pass = $("#txtPass").val();
        var email = $("#txtEmail").val();
        var phone = $("#txtPhone").val();
        var city = $("#txtCity").val();
        var state = $("#txtState").val();
        var zip = $("#txtZip").val();
        $.ajax({
            url: '/EmberNew/Home/Create',
            type: 'POST',
            data: { user_name: user_name, pass: pass,email:email,phone:phone,city:city,state:state,zip:zip },
            success: function (response) {
                alert("success");
            }
        });
        return false;
    }

and its working fine but I want to know that is there any way to pass these values as a single object like in C# forgive me if this question is too silly

serverside code

[HttpPost]
public ActionResult Create(User user)
{
    UserDL newUser = new UserDL();
    newUser.SignUp(user);

    return Json(new { success = true });

}

and also I want to know is there any way to combine these values directly with my server side object

User.cs

public class User
{
    public virtual int ID { get; set; }
    public virtual string UserName { get; set; }
    public virtual string Password { get; set; }
    public virtual string EmailID { get; set; }
    public virtual int Phone { get; set; }
    public virtual string City { get; set; }
    public virtual string State { get; set; }
    public virtual int Zip { get; set; }

}
1

1 Answer 1

2

Try below code. Sore all variable in single object named data and pass it.

function create() {

        var data = {
            'UserName': $("#txtUser").val(),
            'Password': $("#txtPass").val(),
            'EmailID': $("#txtEmail").val(),
            'Phone': $("#txtPhone").val(),
            'City': $("#txtCity").val(),
            'State': $("#txtState").val(),
            'Zip': $("#txtZip").val()
        };
        $.ajax({
            url: '/EmberNew/Home/Create',
            type: 'POST',
            data: data ,
            success: function (response) {
                alert("success");
            }
        });
        return false;
    }
Sign up to request clarification or add additional context in comments.

7 Comments

To get values in your User object, Property name should match with name in JavaScript object and parameter name as well. Can you share your User class definition.
thanks for the response the User class definition added in the question
'function create() { var user = { 'EmailID': $('#emailid').val(), 'password': $('#password').val() } $.ajax({ url: '/EmberNew/Home/Create', type: 'POST', data: { user:user }, success: function (response) { alert("hi"); } }); return false; }' changed the code but not working
In you class definition there are only three properties so you will get those three only in controller. See updated answer
Updated the User model but the problem still there the value binding in script done without any problem but when its pass that object to server it shows null values
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.