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

由网友(我们隔的不只几千公里)分享简介:我有一个Ajax ActionLink的请求字符串中的控制器方法。我想插入字符串转换为超链接的属性。我如何指定目标id元素的属性字段?< IMG ID =CHANGE-MY-SRCSRC =计算机[SRC]><%= Ajax.ActionLink(更改IMG源,actionChange,新AjaxOp...

我有一个Ajax ActionLink的请求字符串中的控制器方法。我想插入字符串转换为超链接的属性。我如何指定目标id元素的属性字段?

 < IMG ID =CHANGE-MY-SRCSRC =计算机[SRC]>

<%= Ajax.ActionLink(更改IMG源,actionChange,新AjaxOptions()
UpdateTargetId =CHANGE-MY-SRC})%>
 

 公共字符串actionChange()
{
   计算机[SRC] =somethingNew;

   返回 ????????
}
 

解决方案

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

创建一个共同的JavaScript文件(加载它的母版页,例如),并添加此功能:

  //创建一个Ajax的onComplete处理程序,将注入
///内容到指定的属性,而非innerHTML的
功能createAttributeInjector(的attributeName){
    复位功能(ajaxContext){
        如果(ajaxContext.get_updateTarget()!== NULL){
            ajaxContext.get_updateTarget()[的attributeName = ajaxContext.get_data();
        }
        //重要提示:燮preSS的默认行为!
        返回false;
    }
}
 
mvc ajax定时刷新数据库,通过Ajax.ActionLink 在ASP.NET MVC5中实现无刷新加载操作方法...

然后,构建Ajax的链接时:

  Ajax.ActionLink(更改IMG源,actionChange,新AjaxOptions(){
    UpdateTargetId =CHANGE-MY-SRC
    OnCompleted =createAttributeInjector(SRC)
}
 

免责声明:我没有能够给这个测试,但我已经做过类似的事情与阿贾克斯佣工。发表在我的回答如果你有问题,我很乐意帮助评论!如果一切正常,让我在评论中知道的!

如果您有源(你可以得到它在codePLEX ),你可以检查出在MicrosoftMvcAjaxScript项目AjaxContext.cs文件,您可以从OnCompleted处理程序访问属性的完整列表

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?

<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 ????????
}

解决方案

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

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

// 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.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!

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

阅读全文

相关推荐

最新文章