简体   繁体   English

cshtml onclick按钮事件并传递一个值

[英]cshtml onclick button event and pass a value

I have been searching for a solution to this I have a button function 我一直在寻找解决方案,我有一个按钮功能

html HTML

<button class="snipcart-add-item btn c8-bg c5-text" onclick="AddProduct(item.Id)">

    @umbraco.library.GetDictionaryItem("USN Shop Add to Basket Listing")
</button>

item.id has got a value as it works in an original code that I are trying to bypass item.id具有值,因为它可以在我尝试绕过的原始代码中使用

java script is Java脚本是

function AddProduct(ptr) {
    $('select').on('click', function () {
        s.ajac({
            url: '/CartSurface/ProductAddToCart',
            data: "{'productId':ptr}",
        }).done(function () {
            alert('Added');
        });
    });
    console.log("Button Pressed")
}

and I get the following error when the button is pressed in the console.log 当在console.log中按下按钮时,出现以下错误

Uncaught ReferenceError: item is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:在HTMLButtonElement.onclick中未定义项目

controller 调节器

public static void ProductAddToCart(int productId)
{
  ......
}

I think I have missed something stupidly simple but I cant see it 我想我已经错过了一些简单的事情,但我看不到

Following Owns comments 关注自己的评论

Thanks Own have changed code to 谢谢自己已将代码更改为

function AddProduct(value) {
        $('select').on("click", function () {
            s.ajac({
                url: '/IBDCartSurface/ProductAddToCart',
                data: "{'productId': value}",
            }).done(function () {
                alert('Added');
            });
        });
        console.log(value);
        console.log("Button Pressed");
    }

<button class="snipcart-add-item btn c8-bg c5-text" onclick="AddProduct(value)" value="@item.Id">

I get the value in the console and the "Button Pressed" message but my break point dont get triggered and the alert dosnt get triggered. 我在控制台中获得了该值,并收到了“ Button Pressed”消息,但是我的断点没有被触发,而警报没有被触发。

Can I ask what is the differance between s.ajac and $.ajax other than the latter cant find the url. 我可以问s.ajac和$ .ajax之间有什么区别,除了后者找不到URL。

Note I have changed the Controller name in the controller as well 注意我也更改了控制器中的控制器名称

Uncaught ReferenceError: item is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:在HTMLButtonElement.onclick中未定义项目

Your button element is passing the item ID as an argument for AddProduct. 您的按钮元素正在传递商品ID作为AddProduct的参数。 The error is telling you that the variable item either doesn't exist or is outside of the current scope. 错误告诉您变量item不存在或不在当前范围内。

Change your JS to something like: 将您的JS更改为:

document.on("click", "#buttonId", function(){
    data = $(this).attr("data-id");
    s.ajac({
        url: '/CartSurface/ProductAddToCart',
        data: "{'productId': data}",
    }).done(function () {
        alert('Added');
    });
}

Then change your html to 然后将您的html更改为

<button class="snipcart-add-item btn c8-bg c5-text" id="buttonId" data-id="item.id">
    @umbraco.library.GetDictionaryItem("USN Shop Add to Basket Listing")
</button>

Have found a different way to do it. 找到了不同的方法来做。 I dont entirely understand all of it but I get the gist of it. 我不完全理解所有内容,但我明白了要点。

Wrap the button in the controller call where the id is the name of the controller function. 将按钮包装在控制器调用中,其中id是控制器功能的名称。 Means not having to translate from one code type to another etc. 意味着不必从一种代码类型转换为另一种代码类型。

values are retrieved from the 2 input tags 从2个输入标签中检索值

csHtml CSHTML

using (Html.BeginUmbracoForm<IBDCartSurfaceController>("SiteWideAddToBasket", null, new { @class = "form", role = "form" }))
{
  <input type="hidden" id="productPageId" name="productPageId" value="@item.Id" />
  <input type="hidden" id="productPrice" name="productPrice" value="@item.ProductCurrentPrice" />
  <button class="snipcart-add-item btn c8-bg c5-text" id="addToBasket" type="submit">
    @umbraco.library.GetDictionaryItem("USN Shop Add to Basket Listing")
  </button>
}

Then the Controller is 然后控制器是

[HttpPost]
public ActionResult SiteWideAddToBasket(int productPageId, decimal productPrice)
{
......
}

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

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