创建动态Html.ActionLink

时间:2011-01-30 18:20:15

标签: ajax asp.net-mvc-2 view controller html.actionlink

我希望在从View中单击按钮后生成动态Html.Action链接。目前我正在使用ajax更新表单上的

标签,其中包含来自数据库的随机生成的产品名称。我的代码如下所示:

<p>
   <%using (Ajax.BeginForm("RandomProductName", new AjaxOptions { UpdateTargetId = "result" }))
    { %>           
        <input type="submit" value="Generate Random Product"/>

 <% } %>
</p>
<p id="result"></p>

控制器中的“RandomProductName”方法返回产品类型的字符串值。我希望能够将其转换为类似于&lt;%= Html.ActionLink(“Pliers”,“Details”,new {id =“2”})%&gt;的动态Html.Action链接。显示详细视图。

我将非常感谢任何有关这方面的帮助,因为我本周末正在学习这个框架。

更新:以下是我当前方法的代码:

public PartialViewResult RandomProductLink()
    {
        int id = RandomProductID();
        Product product = GetProduct(id);

        return PartialView(Product.Name);
        //i think i need to return something like - <%= Html.ActionLink(Product.Name, "Details" new {id=Product.ID})%>
    }

1 个答案:

答案 0 :(得分:0)

<强>更新 现在我可以看到你的代码了,看起来很简单。最简单的方法是更改​​RandomProductName操作以返回将返回用户控件内容的PartialViewResult。

public PartialViewResult RandomProductName()
{
    ProductNameModel productName = GenerateRandomProductName();
    return PartialView(productName);
}

您的模型可能如下所示

public class ProductNameModel
{
   public string Name { get; set; }
   public int Id { get; set; }
}

您的用户控件可能如下所示

<%= Html.ActionLink(Model.Name, "Details", new { id = Model.Id }) %>