如何将ID从对象传递到控制器

时间:2017-03-13 04:51:46

标签: asp.net-mvc

我需要将对象而不是整个对象的视图数据(整数)传递给另一个带整数参数的控制器操作。

查看

@model HWA.ECom.Entity.Customer
@using (Html.BeginForm("Delete", "Customer", FormMethod.Post))
{
    @Html.LabelFor(model => model.Id)
    @Html.EditorFor(model => model.Id)
    @Html.ValidationMessageFor(model => model.Id)

   <input type="submit" value="Submit" />

控制器

public ActionResult Delete(Int32 id)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["EComLocalConnection"].ConnectionString;

        CustomerRepository customerRepository = new CustomerRepository(connectionString);
        return View("ShowMessage", customerRepository.Delete(id));
    }

存储库

public Boolean Delete(Int32 id)
{
    using (SqlConnection con = new SqlConnection(_connectionString))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("usp_Customer_Delete", con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("Id",id);
        Int32 count = cmd.ExecuteNonQuery();
        if (count > 0)
        {
            return true;
        }
    }
    return true;
}

0 个答案:

没有答案