使用MVC中的Html.ActionLink帮助程序将变量从View传递到Controller

时间:2013-05-17 17:39:27

标签: html asp.net-mvc

我正在开发一个项目的增强功能,该项目需要能够删除可能属于一个人的许多地址中的一个。我有一个控制器动作,它根据一个人的id和地址的id来执行此操作。我的问题是在我的Html.ActionLink助手中传递地址id。

我有一个视图,它在sql中为每个人获取多个地址:

 var qryClientAddress = "SELECT    Address.AddressId,AddressType.AddressTypeDescription," +
                           "Address.IsMailing,Address.Street1," +
                           "Address.City," +
                           "Prov.ProvinceDescription," +
                           "Address.Postcode," +
                           "Country.CountryDescription " +
                           "FROM         Address INNER JOIN " +
                           "Person ON Address.PersonId = Person.PersonId INNER JOIN " +
                           " Client ON Person.PersonId = Client.PersonId INNER JOIN " +
                           "Phone ON Person.PersonId = Phone.PersonId INNER JOIN " +
                           "AddressType ON Address.AddressTypeId = AddressType.AddressTypeId INNER JOIN" +
                           "[COMMONDATA].[dbo].[Province] Prov ON  Prov.ProvinceId = Address.ProvinceId INNER JOIN" +
                           "[COMMONDATA].[dbo].[Country] Country ON Country.CountryId = Address.CountryId " +
                           "WHERE     (Person.PersonId) = " + id +
                           " AND Deleted = 0";




    //Load repeater if multiple addresses entered. 
    if (db.ExecuteStoreQuery<ExtendedClient>(qryClientAddress).Count() > 0)
    {
        RepeaterAddress.DataSource = db.ExecuteStoreQuery<ExtendedClient>(qryClientAddress);
        RepeaterAddress.DataBind();

    }

在RepeaterAddress控件中,我根据传递给我的sql语句的id得到属于客户端的地址列表。

 <asp:Repeater ID="RepeaterAddress" runat="server">
       <HeaderTemplate>
    <table class="table1" width="50%">
            <tr>
                <td class="labels displayInput_75w">Type</td>
                <td class="labels displayInput_75w ">Mailing</td>
                <td class="labels displayInput_75w ">Address</td>
                <td class="labels displayInput_75w ">City</td>
                <td class="labels displayInput_75w ">Province</td>
                <td class="labels displayInput_75w " >Postal Code</td>
                <td class="labels displayInput_75w ">Country</td>
             </tr>
            </HeaderTemplate>
            <ItemTemplate>
            <tr>
                 <td><asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("AddressTypeDescription")%>'></asp:TextBox></td>
                 <td><asp:CheckBox ID="chkClientMailing" runat="server" Checked='<%#Convert.ToBoolean(Eval("IsMailing")) %>'/></td>
                 <td><asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("Street1")%>'></asp:TextBox></td>
                 <td><asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("City")%>'></asp:TextBox></td>
                 <td><asp:TextBox ID="TextBox5" runat="server" Text='<%# Eval("ProvinceDescription")%>'></asp:TextBox></td>
                 <td><asp:TextBox ID="TextBox6" runat="server" Text='<%# Eval("Postcode")%>'></asp:TextBox></td>
                 <td><asp:TextBox ID="TextBox7" runat="server" Text='<%# Eval("CountryDescription")%>'></asp:TextBox></td>
                 <td><asp:Label ID="Label1" runat="server" Text='<%# Eval("AddressId")%>'></asp:Label></td>
                 <td><%=Html.ActionLink("Delete", "Delete", new { id = Model.PersonId})%></td> <%--Add addressid--%>
            </tr>

           <%--<asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("")%>'></asp:TextBox>--%>

      </ItemTemplate>
       <FooterTemplate>
                </table>
            </FooterTemplate>
       </asp:Repeater>  

我希望ActionLink传递id和addressid。目前我通过模型从前一个视图的控制器传递id,该控制器按预期工作。但我无法弄清楚如何在我的ActionLink中传递相关的addressId。

<td><%=Html.ActionLink("Delete", "Delete", new { id = Model.PersonId})%></td> <%--Add addressid--%>

我意识到这不是最好的解释,但简而言之,我想传递addressId以及PersonId。

我尝试在我的转发器中创建一个隐藏文本框,并为其分配了addressIds,但我不知道ActionLink中的语法是什么。研究证明没有结果,所以我认为这是不可能的。

有没有更好的方法可以做到这一点,而不会重写太多?

感谢 P

1 个答案:

答案 0 :(得分:0)

嗯,这是一个黑客,但是:

<td><a href='<%=Url.Action("Delete", new { id = Model.PersonId })%>&addressId=<%# Eval("AddressId") %>'>Delete</a></td>