最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get value of a query string from asp.net mvc controller action to <script type="textjavascript">

programmeradmin4浏览0评论

I am passing a query string with name "RowTobeHighLighted" from my Asp 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.

Is it possible to get the querystring value from controller action.Or, is it possible to get the value of viewdata under <script type="text/javascript"> tag.

I am passing a query string with name "RowTobeHighLighted" from my Asp 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.

Is it possible to get the querystring value from controller action.Or, is it possible to get the value of viewdata under <script type="text/javascript"> tag.

Share Improve this question edited Apr 29, 2018 at 15:44 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Aug 14, 2009 at 8:17 Md SamimMd Samim
Add a ment  | 

3 Answers 3

Reset to default 5

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.

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

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论