C# DAL ASP.NET MVC中带out EF的存储过程参数

C# DAL ASP.NET MVC中带out EF的存储过程参数,c#,asp.net-mvc,C#,Asp.net Mvc,编辑:我忘记添加cmd.CommandType=CommandType.StoredProcess。如果忘记了这一行,MVC将表现为从未传递参数 我的数据库中有一个表,我希望使用.NETMVC添加一个记录 DAL码 public void AddCity(City city) { using (var con = new SqlConnection(cs)) { using (var cmd = new SqlCommand("sp

编辑:我忘记添加cmd.CommandType=CommandType.StoredProcess。如果忘记了这一行,MVC将表现为从未传递参数

我的数据库中有一个表,我希望使用.NETMVC添加一个记录

DAL码

public void AddCity(City city)
    {
        using (var con = new SqlConnection(cs))
        {
            using (var cmd = new SqlCommand("spInsertCity", con))
            {
                con.Open();
                //these are the three properties of the City class
                cmd.Parameters.AddWithValue("@cityId", city.CityId);
                cmd.Parameters.AddWithValue("@cityName", city.CityName);
                cmd.Parameters.AddWithValue("@countryId", city.CountryId);
                cmd.ExecuteNonQuery();
            }
        }
    }
控制器

[HttpGet]
        public ActionResult Create()
        {
            return View(new City());
        }
        [HttpPost]
        //pass City object, or form?
        public ActionResult Create(FormCollection form)
        {
            City city = new City();
            city.CityId = Convert.ToInt32(form["CityId"]);
            city.CityName = form["CityName"];
            city.CountryId = Convert.ToInt32(form["CountryId"]);
            var dataAccess = new DataAccessLayer();
            dataAccess.AddCity(city);
            return RedirectToAction("Index");
        }
        public ActionResult Index()
        {
            var dataAccess = new DataAccessLayer();
            var cityList = dataAccess.GetCities();
            return View(cityList);
        }
看法

使用(Html.BeginForm(“创建”、“城市”)){ @Html.ValidationSummary(true) 城市 @LabelFor(model=>model.CityName) @EditorFor(model=>model.CityName) @Html.ValidationMessageFor(model=>model.CityName) @LabelFor(model=>model.CountryId) @EditorFor(model=>model.CountryId) @Html.ValidationMessageFor(model=>model.CountryId) @LabelFor(x=>x.CityId) @EditorFor(model=>model.CityId); @Html.ValidationMessageFor(model=>model.CityId)

对于代码,我得到一个异常,告诉我未提供
@cityId参数
。我的目标是从表单中获取发布的值,该表单应构成
城市
对象,并将其传递给DAL。我有几个问题:

  • 为什么模型绑定器不将文本框的值作为存储过程的参数

  • 在我的DAL中,我应该将参数作为
    City
    对象,即数据库表中三列的三个参数吗


  • FormCollection
    更改为强类型对象,如
    City

    public ActionResult Create(City city)
            {
                var dataAccess = new DataAccessLayer();
                dataAccess.AddCity(city);
    
                return RedirectToAction("Index");
            }
    

    您从表单中读取值的方式意味着您没有使用模型绑定。@QuetiM.Porta不太确定使用FormCollection是否符合模型绑定的条件。无论是哪种方式,由于某种原因,来自表单文本框的值都没有进入存储过程。我的视图是强类型的,以CityI为例,但我仍然得到一个错误,即我的存储过程参数丢失。您是否像查看
    @model city
    那样查看强类型?当您在AddCity中放置断点时,您是否看到哪些变量或属性为空?是的,它是强类型到city的。正在传入正确的数据,但在cmd.exectutenonquiry li上失败ne…当我在SSSMS中使用存储过程时,它可以工作。得到它。这是因为我没有将SqlCommand类型标记为CommandType.StoredProcedure。
    public ActionResult Create(City city)
            {
                var dataAccess = new DataAccessLayer();
                dataAccess.AddCity(city);
    
                return RedirectToAction("Index");
            }