Click here to Skip to main content
15,886,676 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I'm trying to post data to controller using ajax post it contains a model and string,the string value comes to the controller but the model is always null.Can anyone help.Attaching the code

JavaScript
<script>
    function save() {
        var st = "Test";
        var source = ({
            'Name': $('#Name').val(),
            'Code': $('#Code').val(),
            'Address': $('#Address').val(),
            'City': $('#City').val(),
            'State': $('#State').val(),
            'Country': $('#Country').val(),
            'Phone': $('#Phone').val(),
            'Fax':$('#Fax').val()
        });
        $.ajax({
            type: "POST",
            url:'@Url.Action("Save", "Hospitals")',
            data: { 'st': st, 'hospital': source }
        });
    }
</script>

Controller
C#
public ActionResult Save(string st,Hospital hospital)
       {
           if (ModelState.IsValid)
           {
               db.Hospitals.Add(hospital);
               db.SaveChanges();
               return RedirectToAction("Index");
           }
           else
           {
               var dept = db.Departments.ToList();
               ViewBag.department = dept;
           }

           return View(hospital);
       }


Here always the model will be null but will get he string variable.
Posted

Please try as shown below.Use JSON.stringify().

<script>
    function save() {
        var st = "Test";
        var source = ({
            'Name': $('#Name').val(),
            'Code': $('#Code').val(),
            'Address': $('#Address').val(),
            'City': $('#City').val(),
            'State': $('#State').val(),
            'Country': $('#Country').val(),
            'Phone': $('#Phone').val(),
            'Fax':$('#Fax').val()
        });
        $.ajax({
            type: "POST",
            url:'@Url.Action("Save", "Hospitals")',
            contentType: "application/json", 
            data: { 'st': st, 'hospital': JSON.stringify(source) }
        });
    }
</script>


Check for more info: JSON.stringify()
 
Share this answer
 
Comments
Mani89 17-Jun-14 6:04am    
Hi sampath,
Thanks for the immediate reply.I have tried the solution you said but now its going to actionresult for the index page,instead of going to the url mentioned.Then I tried removing "contentType: "application/json" for that it wil go to the url but null value for the model.Can you help me??
Sampath Lokuge 17-Jun-14 7:19am    
Please check this sample.http://stackoverflow.com/questions/5980389/proper-way-to-use-ajax-post-in-jquery-to-pass-model-from-strongly-typed-mvc3-vie
Jquery

JavaScript
<script>
    function save() {
        var cval;
        var str = "";
        $(".chkmaincat:checked").each(function (i) {
            cval = $(this).val();
            str = str + cval + ",";
        });
        alert(str);
        var source =
        {
            'Name': $('#Name').val(),
            'Code': $('#Code').val(),
            'Address': $('#Address').val(),
            'City': +$('#City').val(),
            'State': $('#State').val(),
            'City': $('#City').val(),
            'Country': $('#Country').val(),
            'Phone': $('#Phone').val(),
            'Fax': $('#Fax').val()
        }

        $.ajax({
            type: "POST",
            url: '/Hospitals/Save?st='+str,
            data: source
        });

    }
</script></script>


Controller

JavaScript
public ActionResult Save(string st,Hospital hospital)
       {
           if (ModelState.IsValid)
           {
               db.Hospitals.Add(hospital);
               db.SaveChanges();
               return RedirectToAction("Index");
           }
           else
           {
               var dept = db.Departments.ToList();
               ViewBag.department = dept;
           }

           return View(hospital);
       }


By doing the above i got the string value as well as the modal.Thank you everyone for the help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900