Reputation: 379
I'm developing a small project in C# ASP.NET MVC. My problem is I have a View which presents the user a table with all the Stock. The HTML code is presented below:
<table border="1">
<tr>
<th>Item Id</th>
<th>Brand</th>
<th>Model</th>
<th>Unit Price (€)</th>
<th>Stand</th>
<th>Available Quantity</th>
<th>Operation</th>
</tr>
@foreach (StockViewModel stock in Model.Stock)
{
<tr>
<td>@stock.Item_Id</td>
<td>@stock.Brand</td>
<td>@stock.Model</td>
<td>@stock.Unit_Price</td>
<td>@stock.Stand_Id</td>
<td>@stock.Available_Quantity</td>
<td><a href="~/Stock/UpdateStock/@stock.Item_Id">Update</a> | Delete</td>
</tr>
}
</table>
The problem is the value of the Item_Id is arriving empty in the controller:
public ActionResult UpdateStock(string i)
{
StockViewModel stock = new StockViewModel();
stock.Item_Id = i;
return View("UpdateStock", stock);
}
I already searched for small tutorials on the Internet and they always perform this kind of operations, but mine isn't working. Do you know what I'm missing?
Upvotes: 0
Views: 1464
Reputation: 7490
Without knowing how you have your routes set up, it's impossible to tell - but I'd be willing to bet that you haven't changed the default routes. If that's the case, you can fix this problem by changing UpdateStock(string i)
to UpdateStock(string id)
.
Check out your RouteConfig.cs and look at your default route. The parameter name it expects is id
, and will automatically bind it to whatever follows the "/" on an action. If you need to stick with the variable i
, you can do the following:
<td><a href="~/Stock/[email protected]_Id">Update</a> | Delete</td>
Unless you want to create a custom route, you'll have to build a query string for parameters named anything other than id
.
You may also be interested in looking into Razor's ActionLink HTML Helper, which can be very beneficial when it comes to avoiding typos while creating links.
Upvotes: 2