问题
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" }) %>&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