@Url.Action adding "amp;" between parameters creating nulls in the controller?
Asked Answered
L

1

11

I am trying to send through multiple parameters through the Url.Action.

$('#dialog').dialog({
   autoOpen: false,
   width: 850,
   height: 420,
   resizable: false,
   title: 'Vehicle details',
   modal: true,
   open: function (event, ui) {
   $(this).load("@Url.Action("LightStoneRequest", new { registrationNumber = Model.VehicleRegistration, vinNumber = Model.vVinNumber })");
   },
      buttons: {
          "Close": function () {
               $(this).dialog("close");
           }
       }
   });

During run time it looks as follows:

$(this).load("/APQuotes/LightStoneRequest?registrationNumber=TE5TGP&vinNumber=VINTEST44889856");

enter image description here

As you can see there is a vin number passed through, but its a null in my controller.

Here is my modal.

public partial class LightStoneRequest
    {
        public LightStoneRequest()
        {
            this.LightStoneDataFields = new HashSet<LightStoneDataField>();
        }

        public int LightStoneRequestId { get; set; }
        public string RegistrationNumber { get; set; }
        public string VinNumber { get; set; }

        public virtual ICollection<LightStoneDataField> LightStoneDataFields { get; set; }
    }

if i remove the amp; it works, but the URL.Action adds the amp;.

Landreth answered 2/11, 2015 at 13:56 Comment(3)
This code should work fine. Are you sure that you are seeing a strange behavior with the exact same code ?Observation
@Observation Did not change anything, copy past into the question area and screen shot for proof.Landreth
The answer Luaan gave should work for you.Observation
Z
23

Url.Action isn't the problem - it's how you use it.

You're using @ - this is used for inlining the results of a piece of server-side code in a (X)HTML page. In a (X)HTML page, entities must be properly encoded, turning your & into a &amp;. This is the exact correct behaviour - that's how it's supposed to be inlined in either text or an attribute, for example (which is why you use it in e.g. <a href="@...">).

However, you're trying to inline the raw value, rather than the encoded value - because you're not trying to emit HTML, you're emitting raw text. Html.Raw does just that:

@Html.Raw(Url.Action("Test", new { arg1 = "Hi!", arg2 = "there." }))
Zebedee answered 2/11, 2015 at 14:12 Comment(1)
Oh the pain I went through to finally get here. Ty for the explainationTabulator

© 2022 - 2024 — McMap. All rights reserved.