【问题标题】:How do I pass a specific id to an MVC controller action from jQuery?如何从 jQuery 将特定 id 传递给 MVC 控制器操作?
【发布时间】:2014-10-18 06:49:10
【问题描述】:

我的 html 按钮在最后一行

for (var i = 0; i < len; i++) {
    if (data[i].ProductId && data[i].Name && data[i].ShortDescription && data[i].MediumImage && data[i].Price && data[i].IconImage) {
        //alert(data)
        //alert(Photoq);
        //<img id="imgAd" src="/Images/HomeImages/1.jpg" width="181px" height="215px" alt="img">
        var Photo = "<img id='imgAd' src='" + Photoq + "' width='100px' height='100px' alt='img'/>";
        //alert(Photo);
        txt += '<tr><td><div id ="result1" ><div>' + Photo + '</div> <div ><div>' 
        + '<div id="hello">' + data[i].ProductId + '</div>' + "</br> Name- " 
        + data[i].Name + "</br> Description " 
        + data[i].ShortDescription + ", </br>" 
        + data[i].Price + '<div id="cartremove"><input id="Btnremove" type="submit" value="Remove" /></div>' + "</br>";
    }
}

而我的 jquery 代码是

 $(document).on('click', "#cartremove", function(event) {
     event.preventDefault()
     debugger;
     $.getJSON('@Url.Action("Deletecart", "home")');
 });

我的控制器动作是

public ActionResult Deletecart(int id)
    {
        return view();
    }

我想将特定的 id 传递给控制器​​动作,但我的 jquery $.getjson 没有连接到动作控制器。我怎样才能让它发挥作用?

【问题讨论】:

  • 请更正您的缩进
  • 你能用代码显示吗?
  • 看看有什么缩进this link

标签: jquery asp.net-mvc-4 json.net


【解决方案1】:

Deletecart 需要参数 id 但你没有传递任何东西,你也没有对返回的 html 做任何事情

$.getJSON('@Url.Action("Deletecart", "home")', { id: someValue }, function(data) { 
  // do something with the html that as returned 
});

也建议你改变

<input id="Btnremove" type="submit" value="Remove" />

<button class="Btnremove" type="button">Remove</button>

在这种情况下,您可以删除 e.preventDefault(),因为它没有提交,而且您没有复制 ID

【讨论】:

    猜你喜欢
    • 2018-12-27
    • 2020-08-02
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多