c# - Passing Parameters in URL containing special characters using javascript -


this question has answer here:

hi trying call action result in controller containing parameters, whenever 1 of these parameter contains # sign(special character) string parameters not include # sing in parameter , next parameters set null.

following java script through calling action result.

    <script type="text/javascript">         $(document).ready(function () {             $('#btnexport').unbind().click(function () {                 var url = @url.content("~/bankstatement/exportbankstatementsummary") +                     "?legalname=" + '@viewbag.legalname' +                     "&dba=" + '@viewbag.dba' +                      "&contactperson=" + '@viewbag.contactperson' +                     "&address=" + '@viewbag.address' +                      "&period=" + '@viewbag.period' +                     "&totalheading=" + '@viewbag.totalheading';                 window.location = url;             });         });     </script> 

this action result called in java script

public actionresult exportbankstatementsummary(string legalname, string dba,                                                 string contactperson, string address,                                                 string period, string totalheading)     {         viewbag.legalname = legalname;         viewbag.dba = dba;         viewbag.contactperson = contactperson;         viewbag.address = address;         viewbag.period = period;         viewbag.totalheading = totalheading;          ... 

the problem in action result parameters, when ever of parameter contains special character (# in case) parameter , next parameters becomes null.

for example if address "street # 2" parameter address becomes "street " , next parameters period , totalheading becomes null.

any highly appreciated.

thanks in advance.

[i not agree on duplication of question because marked duplicate question discussed in detail , detailed conversation covered answer of question actual question totally different question.]

you need encodeuricomponent()

the encodeuricomponent() method encodes uniform resource identifier (uri) component replacing each instance of characters one, two, three, or 4 escape sequences representing utf-8 encoding of character (will 4 escape sequences characters composed of 2 "surrogate" characters).

example

 var url = @url.content("~/bankstatement/exportbankstatementsummary") +                 "?legalname=" + encodeuricomponent('@viewbag.legalname') 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -