Passing data to controller while Using URL.Action in jQuery template in Asp.Net MVC dynamic views

若如初见. 提交于 2020-01-06 16:25:31

问题


I have a dynamic view created using jQuery template. The UI is basically a grid with last column being a hyper-linked image. On click on that image i want to pass some data to the controller and that data is present in some other column of the same row. Following is the markup of the code

UI :

{{each Results}}
        <tr>
            <td>${UserName}</td>
            {{if IsActive === 'True'}}
                <td><a href=<%: Url.Action("Test","User",new {userName=???,mode="DeActivate"}) %>><img></img><a></td>
            {{/if}}                
        </tr>
    {{/each}}    

Controller :

public ActionResult Test(string userName,string mode)
    {
    }

Basically I want to associate the ${UserName} value to userName field in Url.Action's parameter list.

Most of the examples I have seen on Url.Action or Html's helper action methods have string data as parameters. I want to know whether it is possible to read data from other controls in the page inside Url.Action parameter list.


回答1:


Try like this:

<a href=<%: Url.Action("Test", "User", new { mode = "DeActivate" }) %>&amp;userName=${$item.urlEncodeUserName()}>
    ...
</a>

where you have defined:

$('#someTemplate').tmpl(contacts, {
    urlEncodeUserName: function () {
        return encodeURIComponent(this.data.UserName);
    }
}).appendTo('#someContainer');


来源:https://stackoverflow.com/questions/5689435/passing-data-to-controller-while-using-url-action-in-jquery-template-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!