我正在尝试使用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
答案 0 :(得分:0)
现在需要一个小版本才能正常工作
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; }
}
}