Asp.net mvc 4 The parameters dictionary contains a null entry for parameter id

Asp.net mvc 4 The parameters dictionary contains a null entry for parameter id,asp.net-mvc-4,Asp.net Mvc 4,function GetStatevalue() { var state_id= $("#ddlstate").val(); var city = $("#txtcity").val(); var datao = new Object(); datao={ state:state_id ,city_name: city } var url = "/city/AddCity"; $.ajax({ url: url, data:datao , cache: false, type: "POST", dataType: "json", success: function (datao) { }, error: function (reponse) { alert("error : " + state_id); } }); Html Form @Html.

function GetStatevalue() {
    var state_id= $("#ddlstate").val();
    var city = $("#txtcity").val();
    var datao = new Object();
    datao={ state:state_id ,city_name: city  }
    var url = "/city/AddCity";
        $.ajax({
        url: url,
        data:datao ,
        cache: false,
        type: "POST",
        dataType: "json",
        success: function (datao) {

        },
        error: function (reponse) {
            alert("error : " + state_id);
        }
    });

Html Form

@Html.DropDownListFor(model => model.tbl_state.coutry_id, (SelectList)ViewBag.country_nm, "Select Country", new {@id="coutry_id" , @onchange="GetState(this.value)"})

<select id="ddlstate" name="ddlstate" style="width: 200px" >
@Html.TextBoxFor(model =>model.tbl_city.city_name ,new {@id="txtcity" })        
<input type="submit" id="save" value="Add" name="actiong" onclick="GetStatevalue();" />

Controller

[HttpPost]
public ActionResult AddCity(int id,String  city_name)
{
   tbl_city data = new tbl_city();
    data.city_name = city_name;
    data.state_id = id;

    db.tbl_city.Add(data);
    db.SaveChanges();
    return Json(data);
}

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult AddCity(Int32, System.String)' in 'test_app.Controllers.cityController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters


#1

After seeing your JqueryCode , you actionresult method should look like:

    [HttpPost]
public ActionResult AddCity(int state,String  city_name)
//bcoz{ state(action variablename):state_id ,city_name(action variablename): city  }
{
   tbl_city data = new tbl_city();
    data.city_name = city_name;
    data.state_id = id;

    db.tbl_city.Add(data);
    db.SaveChanges();
    return Json(data);
}

#2

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult AddCity(Int32, System.String)' in 'test_app.Controllers.cityController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

#3

Because you never send a value of parameter id, only for state and city_name

#4

in a state state_id has been stored. so in controller method use parameter int id, to store the state_id

#5

The data save in database still error page is displayed

#6

Change the method to public ActionResult AddCity(int state, string city_name) or change the script to datao={ id :state_id , city_name: city }!