Domanda

In Razor view I have a Javascript function. This function take 2 URLS String in arguments and call AJAX to do operation. When I generated Url string in Razor, Razor change the URLS. Like changed & to & and damage my query strings which used in my URL address. Also Html.Raw() has not work in this case.

What can I do ?

EXAMPLE:

In my Razor editor:

<a href="#" style="color:#0564c1;" onclick="PopUpStart('POST','','',200,100,'@(Html.Raw(address+"/index/?id="+baseObject.Id+"&"+"type="+dataTypeInt))','@Html.Raw(address + "/cancel/?id="+baseObject.Id+"&type="+dataTypeInt )','ReloadPage',true);return false;">
    Edit
</a>

In result :

<a href="#" style="color:#0564c1;" onclick="PopUpStart('POST','','',200,100,'/PdfInstanceEdit/index/?id=1&amp;type=270','/PdfInstanceEdit/cancel/?id=1`&amp;`type=270','ReloadPage',true);return false;">
    Edit
</a>

The URL address like :

address+"/index/?id="+baseObject.Id+"&"+"type="+dataTypeInt

Change to :

/PdfInstanceEdit/index/?id=1&amp;type=270

In other world character & => &amp;

È stato utile?

Soluzione

Its usually a bad idea to try and combine server code and client strings inside the quotes of a property (ie onclick="something@(something())" )

Its better to just return the entire lot in a server side function

Here's how I would rework your code:

<a href="#" style="color:#0564c1;"                               
onclick="@Html.Raw(
  String.Format(
    "PopUpStart('POST','','',200,100,'{0}','{1}','ReloadPage',true);return false;"
    , Url.Action("index",address,new{id = baseObject.Id, type = dataTypeInt})
    , Url.Action("cancel",address,new{id = baseObject.Id, type = dataTypeInt})
  )
)"/>
Edit
</a>

Also note the difference between @(Html.Raw()) and @Html.Raw() - you should use the latter!

Altri suggerimenti

As direct assignment of events such as onClick is frowned on these days, a better way to accomplish then may be through js:

Add a hidden field for Id and dataTypeInt to your page:

@Html.HiddenFor(model=> model.Id)
@Html.Hidden("dataTypeInt ", dataTypeInt)

Add an id to your anchor:

<a href="#" style="color:#0564c1;" id ="editLink"> Edit </a>

Then your script:

<script>

     $(document).ready(function () {
           readyLinks();
     });

     readyLinks = function(){
          var id = $('#Id).val();
          var dataType = $('#dataTypeInt').val();
          var indexUrl = '/PdfInstanceEdit/index?id=' + id + '&type=' + dataType;
          var cancelUrl = '/PdfInstanceEdit/cancel?id=' + id + '&type=' + dataType;

          $('#editLink).on('click', function(){
               PopUpStart('POST','','',200,100,indexUrl, cancelUrl,'ReloadPage',true);
               return false;
          });

     };

</script>

You should use Html.Raw() as suggested in the comments, see the documentation.

As described in this thread, if you have a particular problem with the output encoded format, you could use the HttpUtility.HtmlDecode() function, see documentation.

@Html.Raw(HttpUtility.HtmlDecode(address+"/index/?id="+baseObject.Id+"&"+"type="+dataTypeInt))

But since this could be a solution I cannot address you problem precisely...

A friendly reminder: if you're trying to put a Javascript string inside an HTML attribute, the value must be encoded twice. It must first be Javascript-encoded, then that result must be HTML-encoded. You could inadvertently open an XSS hole in your site if you don't perform both encodings in the correct order.

Say you have a string s that you want to display to the client. You'd write your Razor markup as:

<a href="#" onclick="alert('Hello, @HttpUtility.JavaScriptStringEncode(s)!')">Click me</a>

Note the explicit call to JavaScriptStringEncode. Then Razor's @ syntax will auto-HtmlEncode this value before writing it to the response.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top