繁体   English   中英

ActionResult 永远不会在 ASP.NET MVC / C# 中调用

[英]ActionResult is never invoked in ASP.NET MVC / C#

我需要更新连接到本地数据库的 ASP.NET MVC 应用程序中特定产品的价格。

ProductController如下,它应该更新数据库的内容:

[HttpPost]
public ActionResult UpdatePrice(Models.Product product)
{
    string query = "UPDATE product SET price=@price  WHERE productId=@idproduct";
      
    string constr = "server=localhost;user id=root;password=;database=accounting;persistsecurityinfo=True";

    using(MySqlConnection con = new MySqlConnection(constr))
    {
        using (MySqlCommand cmd = new MySqlCommand(query))
        {
            cmd.Parameters.AddWithValue("@idproduct", product.idproduct);
            cmd.Parameters.AddWithValue("@price", product.price);
            cmd.Parameters.AddWithValue("@name", product.name);
            cmd.Parameters.AddWithValue("@description", product.description);
            cmd.Parameters.AddWithValue("@cost", product.cost);
            cmd.Parameters.AddWithValue("@quantity", product.quantity);
            cmd.Parameters.AddWithValue("@sku", product.sku);

            cmd.Connection = con;

            con.Open();
            cmd.ExecuteNonQuery();

            Console.WriteLine(product.price);
            con.Close();
        }
    }

    return new EmptyResult();
}

这是产品的视图,在同一个文件中显示所有产品,并允许用户使用 Javascript 编辑产品的价格

<table class="table" id="tblProducts">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.description)
        </th>
       
        <th>
            @Html.DisplayNameFor(model => model.price)
        </th>
       
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td class="Product Name">
                <span>@item.name</span>
                
            </td>
            <td class="Description">
                <span>@item.description</span>
               
            </td>
            <td class="Price">
                <span>@item.price</span>
                <input type="text" value="@item.price" style="display:none; width: 50px;" />
            </td>

            <td>
                <a class="Edit" href="javascript:;"  " >Edit</a>
                <a class="Update" href="javascript:;"  "style="display:none">Update</a>
                <a class="Cancel" href="javascript:;" style="display:none">Cancel</a>
                
            </td>
        </tr>

    }
</table>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="https://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
    <script type="text/javascript">
        //Edit event handler.
        $("body").on("click", "#tblProducts .Edit", function () {
            var row = $(this).closest("tr");
            $("td", row).each(function () {
                if ($(this).find("input").length > 0) {
                    $(this).find("input").show();
                    $(this).find("span").hide();
                }
            });
            row.find(".Update").show();
            row.find(".Cancel").show();
            row.find(".Delete").hide();
            $(this).hide();
        });
        //Update event handler.
        $("body").on("click", "#tblProducts .Update", function () {
            var row = $(this).closest("tr");
            $("td", row).each(function () {
                if ($(this).find("input").length > 0) {
                    var span = $(this).find("span");
                    var input = $(this).find("input");
                    span.html(input.val());
                    span.show();
                    input.hide();
                }
            });
            row.find(".Edit").show();
            row.find(".Delete").show();
            row.find(".Cancel").hide();
            $(this).hide();

            var product = {};
            //product.idproduct= row.find(".productId").find("span").html();
            //product.name = row.find(".Product Name").find("span").html();
            product.price = row.find(".Price").find("span").html();
            $.ajax({
                type: "POST",
                url: "/Product/UpdatePrice",
                data: '{product:' + JSON.stringify(product) + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json"
            });
        });
 </script>

我遇到的问题是价格在 html 页面中更新,我可以看到更改,但实际上没有更改应用于数据库,查看它我只能看到更新,如果我从 MySql 手动更新数据库工作台。 关于我应该做什么的任何建议? 我在 Visual Studio 中放置了断点,似乎产品模型中的价格发生了变化,但数据库中没有任何更新。

在您看来,将产品 id 放入隐藏输入中,以便您可以在发布时将其传递给控制器​​:

...

@foreach (var item in Model)
{
    <tr>
        <td class="Product Name">
            <span>@item.name</span>               
        </td>
        <td class="Description">
            <span>@item.description</span>               
        </td>
        <td class="Price">
            <span>@item.price</span>
            <input type="text" value="@item.price" style="display:none; width: 50px;" />
        </td>

        <td>
            @*Adding a hidden input with the product id on next line*@
            <input type="hidden" value="@item.idproduct" style="display:none;" class="hdn-product-id" />
            <a class="Edit" href="javascript:;"  " >Edit</a>
            <a class="Update" href="javascript:;"  "style="display:none">Update</a>
            <a class="Cancel" href="javascript:;" style="display:none">Cancel</a>                
        </td>
    </tr>
}

现在您可以在按下更新按钮时获取产品的 id,我还为您的 Ajax 添加了successerror回调:

    //Update event handler.
    $("body").on("click", "#tblProducts .Update", function () {
        const row = $(this).closest("tr");
        //now we can get the product id to pass to the controller
        const prodid = $(this).closest('td').find('.hdn-product-id').val();
        
        $("td", row).each(function () {
            if ($(this).find("input").length > 0) {
                var span = $(this).find("span");
                var input = $(this).find("input");
                span.html(input.val());
                span.show();
                input.hide();
            }
        });
        row.find(".Edit").show();
        row.find(".Delete").show();
        row.find(".Cancel").hide();
        $(this).hide();

        var product = {};
        
        product.price = row.find(".Price").find("span").html();
        //set the id to the model
        product.idproduct = prodid;
         
        $.ajax({
            type: "POST",
            url: "/Product/UpdatePrice",
            data: '{product:' + JSON.stringify(product) + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data){
                //do something on success of post
            },
            error: function(jqXHR, exception){
               //do something on error
            }
        });
    });

现在,当您的 Post 方法发生时,您应该拥有要更新的产品的 id,并且您的查询将按预期运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM