1

Hi I am trying to call an action result in my controller containing parameters, but whenever one of these parameter contains # sign(special character) the string parameters does not include # sing in parameter and Next all parameters are set to null.

Following is my java script through which i am calling my 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 is the action result that is called in this 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 is that in the action result parameters, when ever any of the parameter contains any special character (# in this case) then that parameter and next parameters becomes null.

For example if address is "Street # 2" then parameter address becomes "Street " and next parameters period and totalHeading becomes null.

Any help will be highly appreciated.

Thanks in advance.

[I do not agree on the duplication of this question because the marked duplicate question was discussed in detail and the detailed conversation covered the answer of this question but the actual question was totally different from this question.]

Uzair Ashraf
  • 73
  • 2
  • 12
  • `#` is a fragment identifier. The fragment and anything after it will never be sent to the server. You will need to ensure its replace with another character(s). –  Sep 21 '15 at 08:21
  • But why are you building your url like that anyway -instead of `@Url.Action("ExportBankStatementSummary", "BankStatement", new { legalName = ViewBag.LegalName, dba = ViewBag.DBA, .... })` –  Sep 21 '15 at 08:24
  • how do i replace my data containing special characters with other characters plz guide – Uzair Ashraf Sep 21 '15 at 08:26
  • Yes I can do this but whats the benefit in that? – Uzair Ashraf Sep 21 '15 at 08:27
  • See Satpal's answer for one option. And the reason to using `@Url.Action()` is that it will always generate the correct url and internally validates against you route definitions (and its less code and easier to read) –  Sep 21 '15 at 08:29

1 Answers1

2

You need encodeURIComponent()

The encodeURIComponent() method encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).

Example

 var url = @Url.Content("~/BankStatement/ExportBankStatementSummary") +
                "?legalName=" + encodeURIComponent('@ViewBag.LegalName')
Satpal
  • 132,252
  • 13
  • 159
  • 168