将数据保存到数据库时出现错误:
过程或函数'AddName'需要未提供的参数'@CountryName'。
我正在尝试将下拉的所选值文本中的值存储到数据库中。请注意,将数据保存到数据库后出现错误。
我可以使用#Id
存储国家,州和城市的ID,但是我想存储国家,州和城市的名称以及如何解决错误。
我的存储过程:
Create procedure [dbo].[AddName]
(
@CountryName varchar(100),@StateName varchar(100),@CityName varchar(100)
)
as
begin
Insert into DropdownName values(@CountryName,@StateName,@CityName)
End
$("#btnSave").click(function() {
if ($("#CountryId").val() != "" && $("#StateId").val() != "" && $("#CityId").val() != "") {
$.ajax({
type: "POST",//HTTP POST Method
url: "Home/Index",// Controller/View
data: { //Passing data
CountryName: $("#CountryId option:selected").text(),//Reading text box values using Jquery
StateName: $("#StateId option:selected").text(),CityName: $("#CityId option:selected").text(),CountryId: $("#CountryId").val(),StateId: $("#StateId").val(),CityId: $("#CityId").val()
},success: function() {
alert('Data saved');
},error: function() {
alert('Error occurred');
}
});
}
});
@using (html.beginform("Index","Home",FormMethod.Post))
{
@Html.DropDownListFor(m => m.CountryId,Model.Countries,"Please select")
<br />
<br />
@Html.DropDownListFor(m => m.StateId,Model.States,"Please select")
<br />
<br />
@Html.DropDownListFor(m => m.CityId,Model.Cities,"Please select")
<br />
<br />
<input type="submit" value="Submit" id="btnSave" />
}
[HttpPost]
public actionResult Index(CascadingModel model)
{
AddDetails(model);
return View(model);
}
public void AddDetails(CascadingModel obj)
{
string constr = Configurationmanager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
SqlCommand com = new SqlCommand("AddName",con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@CountryName",obj.CountryName);
com.Parameters.AddWithValue("@StateName",obj.StateName);
com.Parameters.AddWithValue("@CityName",obj.CityName);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
}
型号:
public class CascadingModel
{
public CascadingModel()
{
this.Countries = new List<SelectListItem>();
this.States = new List<SelectListItem>();
this.Cities = new List<SelectListItem>();
}
public List<SelectListItem> Countries { get; set; }
public List<SelectListItem> States { get; set; }
public List<SelectListItem> Cities { get; set; }
public int CountryId { get; set; }
public int StateId { get; set; }
public int CityId { get; set; }
public string CountryName { get; set; }
public string StateName { get; set; }
public string CityName { get; set; }
}