0

I have form data which i am trying to post to the server i am using ajax as i have the form as a partial on the page. I have a class that has these parameters which is the "EditModel" class in C# on the server.

How would i convert this to these variables to this model object so that i can pass the data though, this model has to be created using javascript

 function getFromData() {
           //alert("hell yea");
           var username = document.getElementById("Name").value;
           var pa = document.getElementById("password").value;
           var email = document.getElementById("Email").value;
           var active = document.getElementById("IsActive").value;

       }
2
  • 3
    What does this have to do with C#? Commented Apr 12, 2013 at 14:11
  • because the class is on a sever c# Commented Apr 12, 2013 at 14:15

1 Answer 1

1

You need to create a json object to hold the post data before sending it across. Here's an example using jQuery to make the ajax call itself:

var $form = $('#container').find('form'); // this will find the first form element within the element with id of container
var ajaxData = {
    'name': $form.find('#Name').val(),
    'password': $form.find('#password').val(),
    'email': $form.find('#Email').val(),
    'isActive': $form.find('#IsActive').val()
};
$.ajax({
    url: $form.attr('action'),
    type: 'POST',
    data: ajaxData,
    success: function (data, textStatus, jqXHR) {
        if (data.redirect) {
            window.location.href = data.redirect;
        } else {
            alert("Something bad happened");
        }
    }
});

You can make the action method on the server side return some json (via a JsonResult) and check for the presence of that json in the success method of the ajax call. If the json is present, the POST was successful and you can redirect appropriately. I pass a URL in a property called redirect, which I then use to redirect the user to a success screen.

Sign up to request clarification or add additional context in comments.

1 Comment

in the do somthing would i call the method which i have created

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.