[RESOLVED]How to get the id of anchor tag created in mvc razor view.


I am new for MVC, been trying to send the id of the anchor tag clicked to the controller.

<table width="100%">
            @foreach (var item in ViewBag.Schedules as List<BMS.DataAccessLayer.Schedules>)
                {    <td>
                             <span class="span12">
                   <a href="@Url.Action("AgentHomePost")"><img src="@Url.Content(@item.IconUrl.ToString())" width="150px" height="150px" name="@item.BreakTypeName" class=" wip" id="@item.BreakTypeName" alt="@item.BreakTypeName"/> </a> 
 public ActionResult AgentHomePost(string Id)
            // blah blah
            return RedirectToAction("AgentHome", "Home");

I assume that you’ve validated and can see the id’s you want (View Page Source).

The next thing you need to do it put some selection mechanism for the user and encapsulate it all in a @Html.BeginForm block with a submit form.  I would personally not construct the view in the view, I would construct the infrastructure for the view in
the viewmodel and pass a strongly typed view using that viewmodel to the view.  But that’s my preference.

shows a good example.

instead of using <a> tag, try using @html.action, you can pass the input parameters as route values of action link

to List"
{ id=item.BreakTypeName})

To get the image, you need to place a <div> tag around @html.ActionLink and use a css class whose bakground
is the image, and set the width of div to the images width

to List"
{ id=item.BreakTypeName})



you might have to put separate css for different type of url.



You have to use the overload version of the Url.Action(string,object)

In your case, it has to be:
<a href=’@Url.Action("AgentHomePost",new{Id=item.BreakTypeName})’>

check this for overloaded method details:

Hi Ashim,

@Url.Action() is the right solution for this issue. 

let me know if any other issue. 

Leave a Reply