LinkButton in ASP.NET MVC
Asked Answered
U

4

9

I need to instantiate some ASP LinkButtons onto an ASP.NET MVC view page. I've tried a few things, and I cant get them to come out right. Heres my most recent incarnation of the code: the aspx file

<body>
<% using (Html.BeginForm("TitleDetail", "Movies", FormMethod.Post, new{runat="server"})) { %> 

    <ul>
    <% foreach (var disc in Model.Title.tblDiscs) %>
    <% { %>

        <li>
            <asp:LinkButton ID="Play">Link</asp:LinkButton>
        </li>
    <% } %>
    </ul>
<% } %> 
</body>

What it renders in Firefox is one instance of the text Link for each member in the collection I'm enumerating, but they arent hyperlinks, just text. The generated HTML looks like this

<form action="/MyMovies/TitleDetail/239" method="post" runat="server">test title <br />

   <ul>
        <li>

            <asp:LinkButton ID="Play">Link</asp:LinkButton>
        </li>

        <li>
            <asp:LinkButton ID="Play">Link</asp:LinkButton>
        </li>

        <li>
            <asp:LinkButton ID="Play">Link</asp:LinkButton>

        </li>

    </ul>

I've tried adding a runat="server" attribuite to each asp:LinkButton tag, but I get a runtime exception that controls can only be put inside a form tag with the runat="server" attribute. Which I think I've already done, so I dont really understand that. Can anyone explain to me what I'm doing wrong, and what I need to do to fix it so that the LinkButtons are actually linky?

Unattached answered 18/5, 2009 at 22:11 Comment(0)
H
7

With ASP.NET MVC you should use Html.ActionLink(...) helper methods (and the like). They provide much richer functionality than LinkButtons.

Humism answered 18/5, 2009 at 22:23 Comment(0)
A
14

My guess is that you want to use LinkButton to submit the form using it? If that is the case, I have done something similar and it's something like this:

<% using (Html.BeginForm("TitleDetail", "Movies", 
    FormMethod.Post, new{ id = "MyForm", name = "MyForm" })) { %>     
<ul>
    <% foreach (var disc in Model.Title.tblDiscs) { %>
        <li>
            <a class="do-something-link" href="javascript:void(0);">Click</a> 
        </li>
    <% } %>
</ul>
<% } %>

Then handle the submit via jQuery:

$(document).ready(function(){
    $("a.do-something-link").click(function(ev) {
        ev.preventDefault();
        $("#MyForm").submit();
    });
}); 
Arsenate answered 19/5, 2009 at 19:45 Comment(0)
H
7

With ASP.NET MVC you should use Html.ActionLink(...) helper methods (and the like). They provide much richer functionality than LinkButtons.

Humism answered 18/5, 2009 at 22:23 Comment(0)
T
2

Unfortunately, ASP.NET MVC does not support web forms controls right now. Sorry! :(

You'll have to use a standard HTML link.

Transfer answered 18/5, 2009 at 22:20 Comment(2)
I think that's actually a Good Thing (tm). :)Stomatal
@BobbyShaftoe, a good thing on what basis? Normally anything that changes the state of the server should use a POST verb, GET should only be used for retrieving data (those are the recommendations of W3C and they are meant for security). For cosmetic reasons, it's not always possible to use a button, hence the need for LinkButton.Daredevil
P
2

I wrote a somewhat crude extension method. Feel free to improve on this (it's all i needed):

public static IHtmlString ActionButton(this UrlHelper urlHelper, string buttonText,
    string actionName)
{
    var html = string.Format("<input type=\"button\" value=\"{0}\" " +
        "onclick=\"window.location = '{1}'\"/>",
        buttonText, urlHelper.Action(actionName));

    return new HtmlString(html);
}

And in the view, call it like this:

@Url.ActionButton("Button Text", "ActionMethod")
Phago answered 14/6, 2013 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.