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

Hello,

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

<table width="100%">
                    <tr>                  
                       
            @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> 
                   
                 </span>
                        </td>
                   
                }    
                         </tr>
                </table>
 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.

This
post
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

@Html.ActionLink("Back
to List"
,
"AgentHomePost",
new
{ 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

<div
class="ActiveNav">@Html.ActionLink("Back
to List"
,
"AgentHomePost",
new
{ id=item.BreakTypeName})
</div>

.ActiveNav

{width:120px;height:23px;background:url(images/Nav.png)
no-repeat;padding-top:7px;padding-left:10px;}

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

 

Hi,

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:
http://msdn.microsoft.com/en-us/library/dd460294%28v=vs.118%29.aspx

Hi Ashim,

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

let me know if any other issue. 

Leave a Reply