0

I am trying to display information of a certain product depending on the URL with the ID being requested. (for example, if the URL is http://localhost:xxxxx/products/productlist?Id=1) display the view of the product ID.

How can I get the ID from the URL to do an IF statement and display the productID?

@{string value = Request.QueryString["Id"]}
@foreach (var product in Model.prodcuts)
{
     if (product.products == value) 
     {
          <tr>
               <td>@(product.Name)</td>
          </tr>
      }
}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
F.A
  • 297
  • 1
  • 3
  • 13
  • Can you share some code with us, show us what you have tried. This isnt enough for a MCVE https://stackoverflow.com/help/mcve – Webbanditten Mar 21 '17 at 19:24
  • See this http://stackoverflow.com/questions/1033548/retrieve-get-variables-from-url-in-aspx – slayernoah Mar 21 '17 at 19:25
  • var id = URLString.Substring(URLString.LastIndexOf('=') + 1) is a method if you don't want to get the query string directly. But getting the request query string is the best method. The substring is only good if you have only one variable to get, and it's the very last one. – apollosoftware.org Mar 21 '17 at 19:25
  • Possible duplicate of [Retrieve GET variables from URL in ASPX](http://stackoverflow.com/questions/1033548/retrieve-get-variables-from-url-in-aspx) – slayernoah Mar 21 '17 at 19:26
  • The thing is I then use an IF statement but it tells me you cannot compare an int with a string – F.A Mar 21 '17 at 19:31
  • Check which of the values is a an int and use `.ToString()` at the end of whichever is an int? If `product.products` is an int, use. `product.products.ToString()` instead (assuming that `product.products` is the correct attribute that needs to be compared) – slayernoah Mar 21 '17 at 20:04

1 Answers1

0

Assuming product.products is an int: Try changing

@{string value = Request.QueryString["Id"];}

to

@{var value = Convert.ToInt32(Request.QueryString["Id"]);}

This should allow the comparison.

mxmissile
  • 11,464
  • 3
  • 53
  • 79