views:

382

answers:

3

Hi everyone,

I am passing a query string with name "RowTobeHighLighted" from my Asp.net mvc action result. What I have to do is, I have to get the query string value from that controller action to a script of type text/javascript. I have tried to use simple Request.Querystring() under javascript. But that is not working. Can anybody please help me. I am in urgent need. Thanks in advance and sorry for the post if it is not the right place. Is it possible to get the querystring value from controller action.Or, is it possible to get the value of viewdata under tag.

Thank You, Mahammad Samim

+2  A: 

Well no, Request.QueryString won't work, because it's server side only.

You have a few options

  • You could use Request.QueryString to embed the value in a script

    var myValue = <% = HttpUtilityRequest.HtmlEncode(QueryString["myValue"]") %>

  • You could either pass the query string value as view data to the view and then use it in your javascript like so

    var myValue = <% HttpUtilityRequest.HtmlEncode(ViewData["myValue"]) %>

  • Or you could look at the query string in javascript

    var qs = new Querystring() var myValue = qs.get("myValue")

Of course with all of these you should watch for Cross Site Scripting attacks.

blowdart
Thank you very much.I have also found another solution.The solution is as below.<asp:Content ID="Content4" ContentPlaceHolderID="scripts" runat="server"><% var rowToHighLight = Request.QueryString["HighlightedId"];%><script type="text/javascript> In the function $('#designation').setSelection('<%= rowToHighLight %>'), the rowToHighLight is getting value and working fine.So, we can simply use <% var rowToHighLight = Request.QueryString["HighlightedId"];%>To get the value from a controller action or any query string value.Thanks
A: 

On the client side: use the Querystring.
On the server side: (provide value to JS):

<%= "var RowTobeHighLightedUrl = '" + Request.QueryString["RowTobeHighLighted"] + "';"%>

If the RowTobeHighLighted should be JavaScript Escape (NOT HtmlENcode!).

Dmytrii Nagirniak
Thank you very much. I have also found another solution. The solution is as below. <asp:Content ID="Content4" ContentPlaceHolderID="scripts" runat="server"> <% var rowToHighLight = Request.QueryString["HighlightedId"]; %> <script type="text/javascript> In the function $('#designation').setSelection('<%= rowToHighLight %>'), the rowToHighLight is getting value and working fine. So, we can simply use <% var rowToHighLight = Request.QueryString["HighlightedId"]; %> To get the value from a controller action or any query string value.
A: 

Use TempData for this sort of temporary message.

In your controller:

TempData["RowToHighlight"] = rowNumber;

And then in the view:

<% foreach (var row in Model) { %>
<tr>
    <td id="row_<%= row.id %>"<%= (row.id == (int)TempData["RowToHighlight"]) ? " class="highlighted" : "" %>>my row</td>
</tr>
<% } %>

And then if you were using jQuery for example to fade out or whatever (inside your of jQuery document.ready):

<% if (TempData["RoToHighlight"] != null) { %>
$("#row_<%= (int)TempData["RowToHighlight"] %>").fadeOut();
<% } %>

Of course, be sure to use any necessary escaping and encoding etc.

jeef3