0

我想在视图中单击按钮后生成一个动态 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”方法返回产品类型的字符串值。我希望能够将其转换为类似于 <%=Html.ActionLink("Pliers", "Details", new {id = "2"})%> 的动态 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})%>
    }
4

1 回答 1

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 }) %>
于 2011-01-30T18:35:27.197 回答