List getting partially filled. Id is not getting fetched using Dapper

asp.net asp.net-mvc-4 dapper razor stored-procedures

Question

I am trying to populate dropdown list using Dapper but when I am fetching the values from stored procedure ID is not getting fetched only name is getting fetched dont know where I am going wrong. Here is my Code

Controller

public ActionResult Index()
    {
        string str = @"Data Source=DEV_3\SQLEXPRESS;Initial Catalog=DB_Naved_Test;Integrated Security=True";
        SqlConnection con = new SqlConnection(str);
        var para = new DynamicParameters();
        para.Add("@Type", 1);
        var result = con.Query<Region>("Sp_MVCDapperDDl", para, commandType: CommandType.StoredProcedure);
        var list = new SelectList(result,"CountryId","CountryName");
        ViewData["country"] = list;
        return View();
    }

enter image description here

Model

 public class Region
{
    private int _CountryId;
    private string _CountryName;
    public int CountryId
    {
        get { return _CountryId; }
        set { _CountryId = value; }
    }
    public string CountryName
    {
        get { return _CountryName; }
        set { _CountryName = value; }
    }
}

View

@using (Html.BeginForm())
{
@Html.DropDownList("Country", ViewData["country"] as SelectList, "Select Country", new { id = "Country", style = "width: 150px;" })<br />
<select id="State" name="state" , style="width: 150px;"></select><br />
<select id="city" name="City" , style="width: 150px;"></select><br />
}

@Scripts.Render("~/bundles/jquery")
<script type="text/jscript">
  $(function ()
{
    $('#Country').change(function ()
    {        
        $.getJSON('/Cascading/StateList/' + $('#Country').val(), function (data)
        {
            var items = '<option>Select State</option>';
            $.each(data, function (i, state)
            {
                items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
            });
            $('#State').html(items);
        });
    });

    $('#State').change(function ()
    {
        $.getJSON('/Cascading/Citylist/' + $('#State').val(), function (data)
        {
            var items = '<option>Select City</option>';
            $.each(data, function (i, city)
            {
                items += "<option value='" + city.Value + "'>" + city.Text + "</option>";
            });
            $('#city').html(items);
        });
    });
});

Stored Procedure

ALTER procedure [dbo].[Sp_MVCDapperDDl]
 (
 @Type int,
 @CountryId int=0,
 @StateId int=0 
 )
 as
 if @Type=1
 begin
 select * from tbl_country
 end
 if @Type=2
 begin
  select * from tbl_states where cid=@CountryId
 end
 if @Type=3
 begin
 select * from tbl_cities where stateid=@StateId
 end

Accepted Answer

Just a little edition was needed now its working fine

public class Region
{
    private int _cid;
    public int Cid
    {
        get { return _cid; }
        set { _cid = value; }
    }
private string _CountryName;
public string CountryName
{
    get { return _CountryName; }
    set { _CountryName = value; }
}

}



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why