DAL ASP.NET MVC中的存储过程参数w / out EF

时间:2021-07-08 16:39:01

EDIT: I forgot to add cmd.CommandType = CommandType.StoredProcedure. If you forget this line MVC will act like you never passed the parameters.

编辑:我忘了添加cmd.CommandType = CommandType.StoredProcedure。如果你忘记了这一行,MVC的行为就像你从未传递参数一样。

I've got a table in my database that I wish to add a record using .NET MVC

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

DAL Code

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();
            }
        }
    }

Controller

[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);
        }

View

@using (Html.BeginForm("Create","City")) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>City</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.CityName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CityName)
            @Html.ValidationMessageFor(model => model.CityName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CountryId)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CountryId)
            @Html.ValidationMessageFor(model => model.CountryId)
        </div>
        <div class="editor-field">
            @Html.LabelFor(x => x.CityId)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CityId);
            @Html.ValidationMessageFor(model => model.CityId)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

With the code I get an exception telling me that @cityId parameter not provided. I'm aiming to take the posted values from the form that should constitute a City object and pass those to the DAL. I have a couple of question:

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

  1. Why isn't the model binder picking up the text box values as the parameters to my stored procedure?

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

  2. In my DAL should I have the parameter as a City object, our three parameters for the three columns in the database table?

    在我的DAL中我应该将参数作为City对象,我们的三个参数是否为数据库表中的三列?

1 个解决方案

#1


0  

Change the FormCollection to a strongly typed object like City

将FormCollection更改为像City这样的强类型对象

public ActionResult Create(City city)
        {
            var dataAccess = new DataAccessLayer();
            dataAccess.AddCity(city);

            return RedirectToAction("Index");
        }

#1


0  

Change the FormCollection to a strongly typed object like City

将FormCollection更改为像City这样的强类型对象

public ActionResult Create(City city)
        {
            var dataAccess = new DataAccessLayer();
            dataAccess.AddCity(city);

            return RedirectToAction("Index");
        }