1

I try to pass many values to Action in MVC but, user is always null.

var form = $('form');
    $.ajax({
        type: "POST",
        url: '@Url.Content("~/User/UpdateUser")',
        data: { user: form.serialize(), NameOfCare: care },
        success: function (result) {
            if (result == "Ok") {
                document.location.href = '@Url.Content("~/User/Index")';
            }
        }
    });

how to pass form with many values in Ajax ?

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
AddRock
  • 71
  • 2
  • 8
  • 2
    use @Url.Action if UpdateUser is your action method. Moreover, place your action method code here. – Ankita Jan 24 '17 at 14:15
  • You can use the `.param()` method to add additional data - refer [this answer](http://stackoverflow.com/questions/32353093/mvc-jquery-ajax-post-returns-null/32353268#32353268). And as noted by others, it needs to be `url: '@Url.Action(...)'` –  Jan 24 '17 at 20:43

2 Answers2

1

First of all, you have to use @Url.Action instead @Url.Content because you have to send your form data to a controller in order to process data.

.serialize method encode a set of form elements as a string for submission.

You should use this: data:form.serialize() + "&NameOfCare="+care.

On server-side your method should looks like this:

public ActionResult(UserViewModel user,string NameOfCare){

}

user object will be filled with your data , using Model Binding

Community
  • 1
  • 1
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
0

you are sending a serialized object while it's actually a string. try below example. The action will de-serialize the string automatically.

        function load() {
        var dataT = $("#searchform").serialize();
        var urlx = "@Url.Action("SR15Fetch", "SR15", new { area = "SALES" })";
        debugger;
        $.ajax({
            type: "GET",
            url: urlx,    
            data: dataT,  
            cache: false,
            success: HandellerOnSuccess,
            error: HandellerOnFailer,
            beforeSend: Ajax_beforeSend(),
            complete: Ajax_Complete(),
        });
    }
Lloyd
  • 76
  • 2
  • The question is about add an additional name/value pair (`NameOfCare: care`) to the serialized object and this does not address that at all. –  Jan 24 '17 at 22:46