如何在Controller动作中引用Html.TextBoxFor值(更新asp.net-mvc中的表)

问题描述 投票:0回答:2

我正在创建一个销售Web应用程序。我想在输入销售后更新库存中剩余的数量。我为文本框设置了一个id,并在查询中使用了文本框ID。我相信我做错了。

请参阅下面的我的视图和型号代码。 (我对其他如何做到这一点持开放态度)。

我的观看代码:

<div class="container" style="width:40%; margin-top:2%">
@using (Html.BeginForm("SaveSales", "Sales", FormMethod.Post))
{

    @Html.DropDownListFor(model => model.Prod_id, ViewBag.SalesName as        SelectList, "--Select Product--", new { id = "ProdIds", @class = "form-control"        })
    <br />
    @Html.TextBoxFor(model => model.Unit_purchase, new { id = "quant", @class = "form-control", @placeholder = "Amount Purchase" })
    <br />
    @Html.TextBoxFor(model => model.Unit_price, new { @class = "form-control", @placeholder = "Unit Price" })
    <br />
    @Html.DropDownListFor(model =>model.Emp_id, ViewBag.EmpName as SelectList, "--select Employee--", new { @class = "form-control" })
    <br />
    //@Html.TextAreaFor(model => model.Dates, new{@placeholder = "Date", @type = "date", @Value = Model.Dates.ToString("yyyy-MM-dd") })
    @Html.EditorFor(model => model.Dates, new { @type = "date" })
    <br />
    <br />

    @Html.DropDownListFor(model => model.Cust_id, ViewBag.CustName as SelectList, "--Select Customer--", new { @class = "form-control" })
    <br />
    <input type="Submit" value=" Submit" />  <input type="reset" value=" Reset" />
}
 </div>

我的控制器代码(更新查询代码从----开始使用(SqlConnection ......)

    namespace Salesapp.Controllers
     {
     public class SalesController : Controller
      {
        // GET: Sales
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult SalesIndex()
        {
            SalesLayanEntities3 db = new SalesLayanEntities3();
            List<Product> list = db.Products.ToList();
            ViewBag.SalesName = new SelectList(list, "prod_id", "prod_name");

            List<Employee> listi = db.Employees.ToList();
            ViewBag.EmpName = new SelectList(listi, "emp_id", "emp_name");


            List<Customer> listiw = db.Customers.ToList();
            ViewBag.CustName = new SelectList(listiw, "cust_id", "cust_name");

            return View();
        }

        public ActionResult SaveSales(SalesForm model)
        {
            try
            {
                SalesLayanEntities3 db = new SalesLayanEntities3();
                Sales_Record sale_prod = new Sales_Record();

                sale_prod.unit_price = model.Unit_purchase;
                sale_prod.unit_purchase = model.Unit_price;
                sale_prod.prod_id = model.Prod_id;
                sale_prod.emp_id = model.Emp_id;
                sale_prod.Dates = model.Dates;
                sale_prod.cust_id = model.Cust_id;

                db.Sales_Record.Add(sale_prod);
                db.SaveChanges();
                int latestProdId = sale_prod.sales_id;
                TempData["status"] = "Success";

                using (SqlConnection sqlCon = new SqlConnection(@"Data Source=servername;Initial Catalog=SalesLayan;User ID=username;Password=mypassword;"))
                {
                    sqlCon.Open();
                    SqlCommand cmd12 = sqlCon.CreateCommand();
                    cmd12.CommandType = CommandType.Text;
                    cmd12.CommandText = "update product set prod_quantity=prod_quantity-" + quant.Text "where prod_id=" + ProdIds.Text;
                    cmd12.ExecuteNonQuery();
                }

            }

            catch (Exception ex)
            {
                throw ex;


            }
            return RedirectToAction("SalesIndex");
        }
    }  
}
c# sql-server asp.net-mvc
2个回答
0
投票

在您的查询中,您可能有一个不正确的变量参考,用于从库存中扣除的数量。

cmd12.CommandText = "update product set prod_quantity=prod_quantity-" + quant.Text "where prod_id=" + ProdIds.Text;

quant.Text上面的行可能是一个不正确的变量引用。

quant.Text可能需要是sale_prod.unit_pricemodel.Unit_purchase

你的sql命令是设置prod_quantity = prod_quantity - quant.Text

然而,在quant.Text方法中没有变量SaveSales

使用Helper TextBoxFor时,它使用模型名称作为输入字段名称:

示例:Razor View中的TextBoxFor()

@model Student

@Html.TextBoxFor(m => m.StudentName, new { @class = "form-control" })  

Html结果:

<input class="form-control" 
        id="StudentName" 
        name="StudentName" 
        type="text" 
        value="John" />

TextBoxFor

TextBoxFor helper方法是一种强类型扩展方法。它为使用lambda表达式指定的model属性生成文本输入元素。 TextBoxFor方法将指定的模型对象属性绑定到输入文本。因此它会自动在文本框中显示模型属性的值,反之亦然。

参考:

当表单被回发时,.NET MVC使用所谓的模型绑定将表单值组合成Action参数类型:

ASP.NET MVC模型绑定允许您使用模型映射HTTP请求数据。它是使用浏览器在HTTP请求中发送的数据创建.NET对象的过程。不熟悉ASP.Net MVC的ASP.NET Web Forms开发人员大多混淆了View的值在到达Controller类的Action方法时如何转换为Model类,因此这个转换由Model绑定器完成。

参考

关于qazxsw poi注释,将参数传递给sql语句以使用Sql参数是最佳做法,如下例所示:

Joel's

使用参数化的sql命令有助于防止sql注入攻击。一个好的做法也是过滤和验证您的用户输入,因为您在sql update命令中使用它。

参考

  • // 1. declare command object with parameter SqlCommand cmd = new SqlCommand( "select * from Customers where city = @City", conn);

1
投票

看这个部分:

preparing a SqlCommand Object for Parameters

这样做是为了避免sql注入问题并提高性能:

using (SqlConnection sqlCon = new SqlConnection(@"Data Source=servername;Initial Catalog=SalesLayan;User ID=username;Password=mypassword;"))
{
    sqlCon.Open();
    SqlCommand cmd12 = sqlCon.CreateCommand();
    cmd12.CommandType = CommandType.Text;
    cmd12.CommandText = "update product set prod_quantity=prod_quantity-" + quant.Text "where prod_id=" + ProdIds.Text;
    cmd12.ExecuteNonQuery();
}
© www.soinside.com 2019 - 2024. All rights reserved.