列表得到部分填充。没有使用 Dapper 获取 ID
List getting partially filled. Id is not getting fetched using Dapper
我正在尝试使用 Dapper
填充下拉列表,但是当我从 stored procedure
中获取值时 ID
未获取,仅获取名称不知道我在哪里出错了。这是我的代码
控制器
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();
}
型号
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; }
}
}
查看
@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);
});
});
});
存储过程
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
只需要一个小版本就可以正常工作了
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; }
}
}
我正在尝试使用 Dapper
填充下拉列表,但是当我从 stored procedure
中获取值时 ID
未获取,仅获取名称不知道我在哪里出错了。这是我的代码
控制器
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();
}
型号
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; }
}
}
查看
@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);
});
});
});
存储过程
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
只需要一个小版本就可以正常工作了
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; }
}
}