I have an image button which links to a controller. I want to set variable in the url.action by value in an html control in the same view. The code is,
I got one more question. After using window.location.href, the data is passed successfully. But the ajax feature is lost after button click
(The page moves back to top). Is there a way to keep ajax feature?
yzhang738
Member
288 Points
228 Posts
How to set variable in url.action by value in html control
Jul 19, 2012 09:09 PM|LINK
Hi,
I have an image button which links to a controller. I want to set variable in the url.action by value in an html control in the same view. The code is,
<a href="<%: Url.Action("ExportToExcel", "ExportExcelButton", new { page = html.value (id = pagenumberValue) }) %>">
<img src='<%: Url.Content("~/Content/Images/ExportExcelButton.gif") %>' />
</a>
<input type="hidden" id="pagenumberValue" value="#" />
How to do it exactly? Thanks.
CodeHobo
All-Star
18669 Points
2648 Posts
Re: How to set variable in url.action by value in html control
Jul 19, 2012 10:01 PM|LINK
You would have to use JQuery to do this. Something like this:
<script type="text/javascript"> var url = "@Url.Action("ExportToExcel", "ExportExcelButton")"; $(function(){ $("#exportLink").click(function(){ var pagenumberValue = $("#pagenumberValue").val(); window.location.href = url + "?page=" + pagenumberValue; }); }); </script> <a href="#" id="exportLink"> <img src='<%: Url.Content("~/Content/Images/ExportExcelButton.gif") %>' /> </a> <input type="hidden" id="pagenumberValue" value="#" />Blog | Twitter : @Hattan
yzhang738
Member
288 Points
228 Posts
Re: How to set variable in url.action by value in html control
Jul 20, 2012 07:39 PM|LINK
After change to,
var url = "/ExportExcelButton/ExportToExcel";
window.location.href = url + "?resourceId=" + resourceId + "?page=" + pagenum + "?orderBy=" + orderBy + "?filter=" + filter;
The correct string is passed, but as resourceId="123?page=3?orderBy=''?filter=''". Is there a way to pass to different parameters separately?
yzhang738
Member
288 Points
228 Posts
Re: How to set variable in url.action by value in html control
Jul 20, 2012 09:00 PM|LINK
I figure it out. It should be & instead of ?.
yzhang738
Member
288 Points
228 Posts
Re: How to set variable in url.action by value in html control
Jul 22, 2012 09:04 PM|LINK
Hi Hatten,
I got one more question. After using window.location.href, the data is passed successfully. But the ajax feature is lost after button click (The page moves back to top). Is there a way to keep ajax feature?
Thanks,
CodeHobo
All-Star
18669 Points
2648 Posts
Re: How to set variable in url.action by value in html control
Jul 23, 2012 03:04 AM|LINK
What do you mean by the ajax feature is lost?
Blog | Twitter : @Hattan