1
votes

I am trying to turn a button into a Action Link, the link takes me to another page however I need to send new { id = item.Id } to my other page and I don't know how else to do this:

            @foreach (var item in Model.CurrentPost.Tags)
    {
        <div class="col-lg-2">
            <a href="@Html.ActionLink("" + item.Name + "", "GetPostsByTag", "Post", new { id = item.Id }, null)" class="btn btn-default btn-lg">
                @item.Name
            </a>
        </div>

    }

I am getting this error:

A potentially dangerous Request.Path value was detected from the client (<).

When clicking the button the url that it tried to take me to was:

http://localhost:52202/Post/MainDetails/<a href="/Post/GetPostsByTag?id=54">Picture</a>

Have no idea why picture is appended on the end or the href, it should be:

http://localhost:52202/Post/MainDetails/GetPostsByTag?id=54
1
@Html.ActionLink() generates an <a> tag. You do not need to wrap it in another <a> tag. - just use @Html.ActionLink(item.Name, "GetPostsByTag", "Post", new { id = item.Id }, null)user3559349
@StephenMuecke do you know how to solve this? stackoverflow.com/questions/29907772/…G Gr
The first comment in that question should solve the issue (setting the DeleteRule to cascadeuser3559349
Working, thanks @StephenMueckeG Gr

1 Answers

3
votes

@Html is used to generate HTML, matching methods in @Url (like UrlHelper.Action ) should be used to generates urls like href attribute.

So to fix the problem either use @Url.Action if you need detailed control over resulting HTML

<a href='@Url.Action("" + item.Name, "GetPostsByTag", "Post", 
       new { id = item.Id }, null)' class="btn btn-default btn-lg">
            @item.Name
        </a>

Or use @Html.ActionLink to generate whole <a> tag:

@Html.ActionLink(item.Name, "GetPostsByTag", "Post", new { id = item.Id },
         new {class="btn btn-default btn-lg"})