Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to pass the parameters to the controller from location.href.

but link is not hitting the action. location.href seems ok .

What I have tried:

The code i am trying is

the buttion triggering the jqeury code
<button id="getPrice" data-id="" class="btn btn-danger" data-dismiss="modal">GetPrice</button>


the actual jquery code

<pre lang="HTML"> $('#getPrice').click(function () {
                var discountAmount = $('#lblDiscountAmount').text();
                var productId = $(this).parent('td').parent('tr').attr('data-productid')
                alert(productId);
                alert(discountAmount);
                //if (!discountAmount) { return; }

                location.href = "Products/helloPrice?ProductId=" + productId;
               // This is not invoking the action method.

            });

this actionmethod is not triggered when i debug the code and error is resource cant be found and also how to pass addtional parameters
public ActionResult helloPrice(int productid)
      {

          //String a = "produt ID id " + ProductId + "and priec" + ProductPrice;
          //string a = "hi";
          //ViewBag.MyString = "produt ID id " + ProductId +"yes";
          return View();
      }


and also how it can be done with
@Html.ActionLink("HelloPrice", "helloPrice", new { id = item.Id }  , new {@class="myClass"}) 


@class ="myClass" is used to pass additional parameter discountAmount
Posted
Updated 22-Aug-17 21:51pm
v2
Comments
Bryian Tan 22-Aug-17 10:27am    
try
public ActionResult helloPrice(int id)

1 solution

I think that the problem consist in a discordance between the name of the parameters.

In your jquery code you use:
location.href = "Products/helloPrice?ProductId=" + productId;
(Please note that the parameter is ProductId, with both the initial P and the I in uppercase)

but in your controller the parameter is defined:
public ActionResult helloPrice(int productid)
(Please note that all the name is in lowercase)

I think that the solution is just to call the two parameters exactly the same (including upper o lowercas) in both the controller and the jquery code, that is:
a) ProductId in both
b) or productid in both

I hope this will help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900