MVC 3 Ajax.ActionLink并返回helper生成的新元素

时间:2011-12-03 11:14:33

标签: c# asp.net-mvc asp.net-mvc-3

我的页面上有图片:

@Ajax.ImageActionLink(Url.Content("~/Content/images/star-off.png"), "AddToFavourite", "Добавить в избранное", "img_add_to_favourite", null, new AjaxOptions() { HttpMethod = "Post", UpdateTargetId = "img_add_to_favourite" })

由AjaxHelper扩展生成:

public static IHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string actionName, string altText, string ImgId, object routeValues, AjaxOptions ajaxOptions)
{
    var builder = new TagBuilder("img");
    builder.MergeAttribute("src", imageUrl);
    builder.MergeAttribute("alt", altText);
    builder.MergeAttribute("title", altText);
    builder.MergeAttribute("id", ImgId);
    var link = helper.ActionLink("[replaceme]", actionName, routeValues, ajaxOptions).ToHtmlString();
    return new MvcHtmlString(link.Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing)));
}

因此,当我点击此图片时,我会将数据发送到操作。但是,我想用新的'src','alt','title'选项更新这个img,所以,需要返回相同的IHtmlString,由此扩展从动作生成。

是否可以在View中使用exax for AjaxHelper,而在控制器的action方法中使用exension?第一个参数的问题:

public IHtmlString AddToFavourite()
{
    IHtmlString result = Extansions.ImageActionLink(,"", "", "","", null, new AjaxOptions());
    return result;
}

解决方案: out html helper,生成HTML标记:

public static IHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string actionName, string altText, string ImgId, object routeValues, AjaxOptions ajaxOptions)
{
    var builder = new TagBuilder("img");
    builder.MergeAttribute("src", imageUrl);
    builder.MergeAttribute("alt", altText);
    builder.MergeAttribute("title", altText);
    builder.MergeAttribute("id", ImgId);
    var link = helper.ActionLink("[replaceme]", actionName, routeValues, ajaxOptions).ToHtmlString();
    return new MvcHtmlString(link.Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing)));
}

我们的观点:

<div id="update_panel">
@{
    Html.RenderPartial("AddToFavourite", new MVCFairyTales3.Models.ViewModels.ViewModel_AddToFavourite{
        Action="AddToFavourite", 
        ImgId="img_add_to_favourite",
        TaleAn = ((PeopleTale)ViewBag.Tale).Analit,
        TaleType = 1,
        Title = "Добавить в избранное",
        Url = Url.Content("~/Content/images/star-off.png"),
        UserId = (Guid)ViewBag.UserId,
        ajaxOptions = new AjaxOptions() {
            UpdateTargetId = "update_panel",
            HttpMethod = "post"
        }
    }); 
}
</div>

Out Partial View:

@using MVCFairyTales3.Core;
@model MVCFairyTales3.Models.ViewModels.ViewModel_AddToFavourite
@Ajax.ImageActionLink(Model.Url, Model.Action, Model.Title, Model.ImgId, null, Model.ajaxOptions)

我们的控制器返回PartialView,我们将数据传递给html helper:

public ActionResult AddToFavourite()
{
    Guid UserId = new Guid();
    if (User.Identity.IsAuthenticated)
    {
        MembershipUser myObject = Membership.GetUser();
        string sUserID = myObject.ProviderUserKey.ToString();
        UserId = new Guid(sUserID);
    }

    return PartialView("AddToFavourite", new MVCFairyTales3.Models.ViewModels.ViewModel_AddToFavourite
    {
        Action = "DeleteFromFavourite",
        ImgId = "img_delete_from_favourite",
        TaleAn = 1,
        TaleType = 1,
        Title = "Удалить из избранного",
        Url = Url.Content("~/Content/images/star-on.png"),
        UserId = UserId,
        ajaxOptions = new AjaxOptions()
        {
            UpdateTargetId = "update_panel",
            HttpMethod = "post"
        }
    });
}

1 个答案:

答案 0 :(得分:1)

  

是否可以在View中使用AjaxHelper的exension,但是   和控制器的动作方法?

没有。您的控制器操作可能会返回包含对自定义帮助程序的调用的部分内容。