tags:

views:

711

answers:

4

Hi,

I'm trying to implement something like this:

<div>
    <table>
        <thead>
            <tr>
                <td>Port name</td>
                <td>Current port version</td>
                <td>New port version</td>
                <td>Update</td>
            </tr>
        </thead>
        <% foreach (var ip in Ports) { %>
        <tr>
            <td><%= ip.PortName %></td>
            <td><%= ip.CurrentVersion %></td>
            <td><%= ip.NewVersion %></td>
            <td><asp:Button ID="btnUpdate" runat="server" Text="Update" CommandArgument="<% ip.PortName %>" /></td>
        </tr>
        <% } %>
    </table>
</div>

The button's CommandArgument property is where my code complains about not being able to resolve symbol 'ip'. Is there any way to do what I'm trying to do?

+3  A: 

You don't want to use a Webforms button in ASP.NET MVC. MVC is a completely different way of working, and you no longer have the WebForms abstraction.

You have 2 different options you can either replace your asp:Button with an input tag or use a standard hyperlink instead. If you use the input option then you will need to wrap in a form element. The form action should point to a Controller action.

Andrew Rimmer
A: 

I think you have to enclose your block in Form tags ans runat=server.

+1  A: 

You can't use webform controls in ASP.NET MVC in a trivial manner because they rely on things that are stripped out in MVC. Instead you add a button in two ways, both using the HtmlHelper on the ViewPage:

You can add a button in a form, which is easily handeled in a controller if you have a form for each single button:

<% using(Html.BeginForm("Update", "Ip", new {portName = ip.PortName} )) { %>

    ....
    <input name="action" type="submit" value="Update">

<% } %>

BeginForm() will default to the same controller and action as the view was created from. The other way is to add a link instead, which is more fitting to your example of iterating through a list. For example lets say you have IpController

<%= Html.ActionLink("Update IP", "Update", "Ip", 
        new { 
            portName = ip.PortName 
        }) 
%>

The link will go to the Update action in IpController with the given portName as parameter. In both cases you'll need this action in IpController:

public ActionResult Update(string portName) {
    // ...
}

Hope this helps.

Spoike
A: 

FWIW,

I think this text is missing an equals sign:

CommandArgument="<% ip.PortName %>"

Should be

CommandArgument="<%= ip.PortName %>"

Tim Rourke