ASP.NET MVC:AJAX ActionLink- 以 HTML 属性为目标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/488726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 11:06:40  来源:igfitidea点击:

ASP.NET MVC : AJAX ActionLink- Target an HTML attribute

.netasp.net-mvcasp.net-ajax

提问by zsharp

I have an Ajax actionlink that requests a string in the controller method. I want to insert that string into an attribute of a hyperlink. HOw do I specify the attribute field of the target id element?

我有一个 Ajax actionlink,它在控制器方法中请求一个字符串。我想将该字符串插入到超链接的属性中。如何指定目标 id 元素的属性字段?

<img id="CHANGE-MY-SRC" src=ViewData["src"] >

<%=Ajax.ActionLink("Change IMG Source","actionChange",new AjaxOptions()         
UpdateTargetId="CHANGE-MY-SRC"})%>


public string actionChange()
{
   ViewData["src"]= "somethingNew";

   return ????????
}

回答by Andrew Stanton-Nurse

The default behavior for the Ajax helpers won't support this. You can, however, create a custom JavaScript handler that is run when the Ajax request returns, and then use that to inject the value into the attribute

Ajax 助手的默认行为不支持这一点。但是,您可以创建一个在 Ajax 请求返回时运行的自定义 JavaScript 处理程序,然后使用它来将值注入到属性中

Create a common JavaScript file (load it up in the Master page, for example) and add this function:

创建一个通用的 JavaScript 文件(例如在母版页中加载它)并添加以下函数:

// Creates an Ajax OnComplete handler that will inject 
///the contents into the specified attribute, rather than the InnerHtml
function createAttributeInjector(attributeName) {
    return function(ajaxContext) {
        if(ajaxContext.get_updateTarget() !== null) {
            ajaxContext.get_updateTarget()[attributeName] = ajaxContext.get_data();
        }
        // IMPORTANT: Suppress the default behavior!
        return false;
    }
}

Then, when building your Ajax link:

然后,在构建 Ajax 链接时:

Ajax.ActionLink("Change IMG Source", "actionChange", new AjaxOptions() {
    UpdateTargetId="CHANGE-MY-SRC", 
    OnCompleted="createAttributeInjector('src')"
}

Disclaimer: I haven't been able to give this a test, but I've done similar things with the Ajax helpers. Post in the comments for my answer if you have problems and I'm happy to help! If it works, let me know in the comments as well!

免责声明:我无法对此进行测试,但我已经对 Ajax 助手做过类似的事情。如果您有问题,请在评论中发表我的回答,我很乐意为您提供帮助!如果有效,请在评论中告诉我!

If you have the source (you can get it on CodePlex), you can check out the AjaxContext.cs file in the MicrosoftMvcAjaxScript project for a full list of the properties you can access from the OnCompleted handler

如果您有源代码(您可以在 CodePlex 上获得),您可以查看 MicrosoftMvcAjaxScript 项目中的 AjaxContext.cs 文件,以获取您可以从 OnCompleted 处理程序访问的属性的完整列表

回答by tvanfosson

I'm not sure exactly what you mean but you can provide a set of HTML attributes some versions of the ActionLink extension.

我不确定您的意思,但您可以为某些版本的 ActionLink 扩展提供一组 HTML 属性。

Ajax.ActionLink( string linkText,
                 string action,
                 object routeValues,
                 AjaxOptions ajaxOptions,
                 object htmlAttributes )

Example:

例子:

<%= Ajax.ActionLink( "add",
                     "AddThing",
                     new { id = ViewData.Model.ID },
                     new AjaxOptions { Confirm = "Are you sure?",
                                       UpdateTargetId = "thingList"
                                     },
                     new { title = "Add a thing to the database." } ); %>

回答by Fabio Milheiro

Much simpler I guess, you could use Url.Action (the action you set the one that returns the string you want, but instead of returning you use Response.Write(...).

我猜要简单得多,您可以使用 Url.Action (您设置的操作返回您想要的字符串,但您使用 Response.Write(...) 而不是返回。

Or even simpler, you use src="<%= ViewData["src"] %>" in your tag!

或者更简单,您在标签中使用 src="<%= ViewData["src"] %>" !

I see you already have a solution, but this could save you time next time..

我看到您已经有了解决方案,但这可以节省您下次的时间..