1

I try to send a object from the form to a HttpPost method(who recives a object parameter) but i get always a NullExceptionReferece but if I pass as string I don't get the error. My controller:

[HttpPost]
    //public ActionResult CheckVat(string vatnumber) {
    public ActionResult CheckVat(VatSearch vatnumber)
    {
        return Json(vatnumber);
    }

my ajax function:

<h3>Insert VAT number to check:</h3>
@model LatourretrVat.Models.VatSearch

<form id="form">
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                @Html.TextBoxFor(m => m.VatNumber,new { @class = "form-control", @id="VatNumber"})
                 @Html.ValidationMessageFor(model => model.VatNumber, "", new { @class = "text-danger" })
             </div>
        </div>
        <div class="col-sm-6">
             <div class="form-group">
                <input type="submit" class="btn btn-primary" value="Check VAT" />
            </div>
        </div>
   </div>
</form>
<script >
    $(document).ready(function () {
        $('#form').submit(function () {
            var VatSearch = new Object();
            VatSearch.VatNumber = $("#VatNumber").val();
            $.ajax({
                url: '@Url.Action("CheckVat", "Home")',
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                //data: JSON.stringify({
                //    VatNumber: $('#VatNumber').val(),
                //}),
                data: JSON.stringify(VatSearch),
                success: function (result) {
                    alert("success");
                },
                error: function (result) {
                    console.log(result);
                    alert("error");
                }
            });
          return false;
        });
    });
</script>
Fabio Santos
  • 253
  • 1
  • 4
  • 15
  • Have you tried serializing the form instead? `data: $('#form').serialize(),` – JB06 Sep 07 '16 at 18:41
  • See related url [http://stackoverflow.com/questions/13242414/passing-a-list-of-objects-into-an-mvc-controller-method-using-jquery-ajax](http://stackoverflow.com/questions/13242414/passing-a-list-of-objects-into-an-mvc-controller-method-using-jquery-ajax) [http://stackoverflow.com/questions/16271404/how-to-pass-an-object-to-controller-using-ajax-call](http://stackoverflow.com/questions/16271404/how-to-pass-an-object-to-controller-using-ajax-call) – Jack1987 Sep 07 '16 at 18:42
  • I already try the sugestions but still not working, – Fabio Santos Sep 07 '16 at 18:48
  • Can you let us see your html code? – DCruz22 Sep 07 '16 at 18:49
  • I update the post with HTML – Fabio Santos Sep 07 '16 at 19:06

2 Answers2

1

Your HttpPost action method parameter's name should not be same as one of the properties of the parameter type.

Currently your user vatnumber as the parameter type and i assume your view model also has a property called VatNumber.

This should work.

[HttpPost]
public ActionResult CheckVat(VatSearch data)
{
   return Json(data);
}

Also, there is no need to specify the dataType as "json" as you are explicitly sending json response from the server.

Shyju
  • 214,206
  • 104
  • 411
  • 497
0

If you want to receive and object in your controller you can send a JSON object like this:

    $('#form').submit(function () {
        var VatSearch = { "VatNumber" : $("#VatNumber").val() };
        $.ajax({
            url: '@Url.Action("CheckVat", "Home")',
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(VatSearch),
            success: function (result) {
                alert("success");
            },
            error: function (result) {
                console.log(result);
                alert("error");
            }
        });
      return false;
    });
DCruz22
  • 806
  • 1
  • 9
  • 18