zsh*_*arp 5 .net asp.net-mvc asp.net-ajax
我有一个Ajax actionlink,它在控制器方法中请求一个字符串.我想将该字符串插入到超链接的属性中.我是否指定了目标id元素的属性字段?
<img id="CHANGE-MY-SRC" src=ViewData["src"] >
<%=Ajax.ActionLink("Change IMG Source","actionChange",new AjaxOptions()
UpdateTargetId="CHANGE-MY-SRC"})%>
Run Code Online (Sandbox Code Playgroud)
public string actionChange()
{
ViewData["src"]= "somethingNew";
return ????????
}
Run Code Online (Sandbox Code Playgroud)
Ajax帮助程序的默认行为不支持此操作.但是,您可以创建在Ajax请求返回时运行的自定义JavaScript处理程序,然后使用该处理程序将值注入属性
创建一个通用的JavaScript文件(例如,在Master页面中加载它)并添加此功能:
// 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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,在构建Ajax链接时:
Ajax.ActionLink("Change IMG Source", "actionChange", new AjaxOptions() {
UpdateTargetId="CHANGE-MY-SRC",
OnCompleted="createAttributeInjector('src')"
}
Run Code Online (Sandbox Code Playgroud)
免责声明:我无法对此进行测试,但我已经使用Ajax助手做了类似的事情.如果您遇到问题,请在评论中发帖,我很乐意为您提供帮助!如果有效,请在评论中告诉我!
如果您有源代码(可以在CodePlex上获取),可以查看MicrosoftMvcAjaxScript项目中的AjaxContext.cs文件,以获取可以从OnCompleted处理程序访问的属性的完整列表.
| 归档时间: |
|
| 查看次数: |
28453 次 |
| 最近记录: |