将AjaxHelper.ActionLink从服务器传递到模型中的视图? [英] Pass AjaxHelper.ActionLink from the server to the view in a model?

查看:75
本文介绍了将AjaxHelper.ActionLink从服务器传递到模型中的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET MVC 3中,我希望能够在服务器上创建一个枚举的AjaxHelper.ActionLink,将其添加到模型中,然后将该模型绑定到视图.该视图将具有动态数量的ActionLink.

In ASP.NET MVC 3, I would like to be able to create an enumerable of AjaxHelper.ActionLink on the server, add it to a model, and bind the model to a view. The view would then have a dynamic number of ActionLink.

当我尝试在控制器操作中实例化一个新的AjaxHelper时,它需要诸如ViewContext和IViewDataContainer之类的东西,所以我有些困惑.

When I try to instantiate a new AjaxHelper in my controller action, it needs things like a ViewContext and an IViewDataContainer, so I'm getting a little stumped.

理想情况下,我希望有一个工厂来确定如何构造ActionLink,构造多少并返回ViewResult或ActionResult.

Ideally I would like to have some factory that does the work of determining how to construct the ActionLink, how many to construct, and which returns a ViewResult or ActionResult.

我已经进行了一些挖掘,但是似乎找不到这种可能性或可​​接受的模式...

I've done some digging around, but can't seem to find if this is possible or an acceptable pattern...

希望这很清楚,但是我很高兴澄清一下!

Hopefully this is pretty clear, but I'm happy to clarify if not!

推荐答案

创建名为的自定义视图

AjaxLinksPartial.cshtml

AjaxLinksPartial.cshtml

@Scripts.Render("~/bundles/jquery") @*You need this included, probably in your main layout*@
@Scripts.Render("~/bundles/jqueryval")
<div id ="Target"></div>

@{foreach (var link in Model.LinkItems)
 {
    @Ajax.ActionLink(link.Text, link.Action, link.Controller, null, Model.AjaxOptions, Model.HtmlAttrbs)
 }
}

具有支持模型AjaxLinks

with a backing model AjaxLinks

public class AjaxLinks
{
    public List<LinkItem> LinkItems { get; set; }
    public AjaxOptions AjaxOptions { get; set; }
    public Dictionary<string, Object> HtmlAttrbs { get; set; }

    public AjaxLinks(AjaxOptions options, Dictionary<string, Object> htmlAttrbutes)
    {
        LinkItems = new List<LinkItem>();
        this.AjaxOptions = options;
        this.HtmlAttrbs = htmlAttrbutes;
    }
}
public class LinkItem
{
    public string Text { get; set; }
    public string Controller { get; set; }
    public string Action { get; set; }

    public LinkItem(string Text, string Controller, string Action)
    {
        this.Text = Text;
        this.Controller = Controller;
        this.Action = Action;
    }
}

在控制器中按以下方式使用

use it as follows in the controller

        var htmlAttributes = new Dictionary<string, Object>();
        htmlAttributes.Add("class", "linkstyling class");
        var ajaxOptions = new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "Target", HttpMethod = "POST"};
        var links = new AjaxLinks(ajaxOptions, htmlAttributes);
        links.LinkItems.Add(new LinkItem("About","Home","About"));

并将其呈现在另一个视图中

and render it within another view

 @Html.Partial("AjaxLinksPartial", Model)

主要缺点是,这不是样式和其他方面的明确区分.如果您能够使用HtmlHelpers彻底重做,那么我将非常感兴趣.pm我或发表评论.

the main downside is this isn't a clean separation of styling and other concerns. If you are able to remake this cleanly using HtmlHelpers, I would be very interested. PM me or comment if you do.

这篇关于将AjaxHelper.ActionLink从服务器传递到模型中的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆