tahtoh
tahtoh

Reputation: 102

How to remove encoded characters when serializing an input array?

I have my partial view which contains this:

<div class="multiplePaths">
  @for (var i = 0; i < Model.Count; i++)
  {
     <div class="form-group">
        @Html.HiddenFor(model=>model[i].Label)
        @Html.EditorFor(model => model[i].Path, new {@class = "form-control col-md-10"})
     </div>
  }
</div>

When I call this jQuery:

var model = $(".multiplePaths :input").serialize();

The results look something like this:

%5B0%5D.Label=Repertoire%20destination&%5B0%5D.Path=

Upvotes: 0

Views: 1033

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337656

The formatting is because serialize() is intended to output a querystring. As such, special characters, such as [ and ], are URI encoded.

To get the output you want you simply need to URI decode them again:

var model = $(".multiplePaths :input").serialize();
console.log('encoded:', model);

var decodedModel = decodeURI(model);
console.log('decoded:', decodedModel);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="multiplePaths">
  <div class="form-group">
    <input type="hidden" name="[0].Label" value="fooLabel_1" />
    <input type="text" name="[0].Path" value="fooPath_1" />
  </div>
</div>

Upvotes: 3

Related Questions