参数字典包含参数id的空条目

时间:2016-06-25 06:20:12

标签: 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表格

@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();" />

控制器

[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);
}
  

参数字典包含'test_app.Controllers中方法'System.Web.Mvc.ActionResult AddCity(Int32,System.String)'的非可空类型'System.Int32'的参数'id'的空条目。 cityController”。可选参数必须是引用类型,可空类型,或者声明为可选参数。参数名称:参数

1 个答案:

答案 0 :(得分:0)

看到你的JqueryCode之后,你的actionresult方法应该如下所示:

    [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);
}