Fabio Santos Fabio Santos - 1 month ago 5x
Ajax Question

ASP net 4 send object from ajax nullexceptionReference

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:

//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 class="col-sm-6">
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Check VAT" />
<script >
$(document).ready(function () {
$('#form').submit(function () {
var VatSearch = new Object();
VatSearch.VatNumber = $("#VatNumber").val();
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) {
error: function (result) {
return false;


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.

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.