简体   繁体   中英

MVC 3 Ajax.ActionLink and return new element generated by helper

I have on my page image:

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

generated by extension of 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)));
}

So, when i click on this image, i send data to action. But then, i'd like to update this img with new 'src', 'alt', 'title' options, so, need to return the same IHtmlString, generated by this extension from action.

Is it possible to use exension for AjaxHelper not only in View, but and in action method of controller? Problem with first argument:

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

Solution : Out html helper, that's generate HTML markup:

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)));
}

Our view:

<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)

And our controller, which return PartialView, where we pass data to call 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"
        }
    });
}

Is it possible to use exension for AjaxHelper not only in View, but and in action method of controller?

No. Your controller action could return a partial containing a call to the custom helper.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM