HTML.Actionlink with a Specified HTML Id

Html.ActionLink with a specified HTML id?

You were on the right track. I am not sure why it didn't work for you as your code has a typo which would have produced a } expected error. The following is what you are looking for:

 <%= Html.ActionLink("Test Link", "SomeAction", "SomeController",
null, new {id = "someID" }) %>

Which produces teh following HTML:

<a href="/SomeController/SomeAction" id="someID">Test Link</a>

Edit: I just realized what the issue is because I was mis-reading what you tried. You are using the wrong overload to pass in the id html element. Your probably passing the new { id="blah" } param into the routeValues parameter, which will cause it to be used when building the route link, rather than the htmlAttributes paramater which is what you want.

I think you are using:

ActionLink(string linkText, string actionName, Object routeValues,
Object htmlAttributes)

When what you need to use is the following overload like I did above in my answer:

ActionLink(string linkText, string actionName, string controllerName,
Object routeValues, Object htmlAttributes)

Which makes sure new { id="blah" } is being passed into the htmlAttributes param.

Html.Actionlink with ID

I suspect you don't have a route configured for Home/Blog/{BlogId}. So if you want to be able to write this (as what you show in your code):

@Html.ActionLink("Back To Blog","Blog","Home", new { Model.BlogId}, null)

You need to have a route like this:

routes.MapRoute(
name: "DefaultBlog",
url: "{controller}/{action}/{blogid}",
defaults: new { controller = "Home", action = "Blog",
blogid = UrlParameter.Optional }
);

Notice the third part of the url which is named blogid. But if you don't want to configure another route then you can just rewrite your link as:

@Html.ActionLink("Back To Blog","Blog","Home", new { id = Model.BlogId}, null)

Notice the id on the parameter.

How to add a id attribute to @Html.ActionLink?

There are two ways.

1) use Id attribute

2) use html5 data attribute.

Razor:

@Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { data_id = item.Id, id = item.Id })

Jquery:

$('a').click(function(){

alert($(this).data("id")); // alerts data-id attribute

//or

alert(this.id); // this alerts Id attribute

});

ASP.NET MVC passing an ID in an ActionLink to the controller

Doesn't look like you are using the correct overload of ActionLink. Try this:-

<%=Html.ActionLink("Modify Villa", "Modify", new {id = "1"})%>

This assumes your view is under the /Views/Villa folder. If not then I suspect you need:-

<%=Html.ActionLink("Modify Villa", "Modify", "Villa", new {id = "1"}, null)%>

MVC HTML Action Link Post ID

You need to add id= before the parameter:

 @Html.ActionLink(@skl.userskill_content ,   
"Index" , "Home" , new {id = skl.userskill_id} , null);

You must also make sure that the controller action is an HTTP get as an ActionLink will invoke this, and also make sure your controller parameter matches i.e

public ActionResult AnAction(int id)

How to Add ID to Html.ActionLink - Same as You Would In HTML / CSS

Try this:

@Html.ActionLink("LION TECHNOLOGIES", "Index", "Home", new { area = "" }, new { @class = "navbar-brand", @id = "navbar-text"})

Html.ActionLink sending Id parameter to another controller

You should change it to:

<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Deposit", "Deposit", "Transaction",new {id = item.Id }, null) |
@Html.ActionLink("Withdraw", "Withdraw", "Transaction", new { id = item.Id }, null) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>

Note the last null parameter. ActionLink helper have a little problems with lots of overloads so without null you basically use wrong overload that's why you don't get link that you need.

Here is a full answer:

Here's what you are using:

@Html.ActionLink(
"Reply", // linkText
"BlogReplyCommentAdd", // actionName
"Blog", // routeValues
new { // htmlAttributes
blogPostId = blogPostId,
replyblogPostmodel = Model,
captchaValid = Model.AddNewComment.DisplayCaptcha
}
)

and here's what you should use:

@Html.ActionLink(
"Reply", // linkText
"BlogReplyCommentAdd", // actionName
"Blog", // controllerName
new { // routeValues
blogPostId = blogPostId,
replyblogPostmodel = Model,
captchaValid = Model.AddNewComment.DisplayCaptcha
},
null // htmlAttributes
)

Html. ActionLink: Passing CSS class and Id at the same time

So, I tried this to make it work

 <td>@Html.ActionLink("Sprawdź","Details","Home", new { @item.Id }, new {@class = "btn btn-info" })</td>

Created two annonymous classes, the first one passing Id, and second one passing the class. I leave the topic, maybe someone will find this helpful :)



Related Topics



Leave a reply



Submit