0

So basically as the title said, currently working on a MVC5, how can I get the Id of the list of Items that I have in view of the controller? By a button click, I'm calling an action which doesn't have a view but requires the Id of the item, which is resulting in a NULL.

I read this article but they don't seem to work for me:

How to get the current user in ASP.NET MVC

 public ActionResult GetItem(int Id)
 {
     Item item = new Repository().GetId(Id);
     ..
 }
User89
  • 93
  • 1
  • 8
  • Can you share some code? – Nemanja Todorovic Dec 27 '19 at 10:40
  • @NemanjaTodorovic, this doesn't really require a demonstration of my code, but I'll edit it. – User89 Dec 27 '19 at 10:43
  • @User89 Please show us your `View` code. How are you sending the `Id` to your Controller? – Rahul Sharma Dec 27 '19 at 11:25
  • @RahulSharma I think you didn't understand my question. I have a Page witch consists of a table of items with name and other fields. Now I have a button called Get item which it's supposed to get that item by its ID. Thus, right now when I'm clicking the button it's giving me an error in the parameter stating that the Id is null. – User89 Dec 27 '19 at 13:01
  • @User89 I completely do understand your question. The issue lies in how you are binding your value(Id) and then sending it to the Controller to get your required. Are you using Model-Binding or AJAX to send your value? – Rahul Sharma Dec 27 '19 at 13:12
  • @RahulSharma I'm sending it as Model-Binding (model.id) – User89 Dec 27 '19 at 13:20
  • @User89 Try this: `public ActionResult GetItem(int id)` – Rahul Sharma Dec 27 '19 at 13:22
  • @RahulSharma It's exactly how I'm doing it.. – User89 Dec 27 '19 at 13:22
  • @User89 Notice the `int id` in my case and `int Id` in your case – Rahul Sharma Dec 27 '19 at 13:23
  • @RahulSharma I have just tried it. Stil it doesn't work. I've read on how to get a Users name but regarding getting an Id I cant find anything – User89 Dec 27 '19 at 13:25
  • @User89 I guess you are missing out on the point. The link in your question corresponds to an `Identity` which is when a user is successfully authenticated, the context is made with its properties. Now in your case, you have a list of items that have corresponding `id`. When you click on a button, you want that item's `id` to be sent to your Controller method which will return a result (JSON). In order to do that, I need to see what action do you take when you click on the button. – Rahul Sharma Dec 27 '19 at 13:29

1 Answers1

1

Okay, so after a discussion on the requirement, the following changes were required to be made on the View:

<div class="col-lg-4">
<a href="@Url.Action("GetItem", "Upload", new { id = Model.id}"">
</a>
<P>@p.Path</P>
</div>

And the Controller:

public ActionResult GetItem(int id)

The URL for each item would be generated uniquely based on the Model.id

Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54