Note that there are some explanatory texts on larger screens.

plurals
  1. PONull values when invoking MVC controller from Javascript
    text
    copied!<p>In my MVC view model, I have a table, inside a form. </p> <p>From my javascript, I'm invoking my controller as follows: </p> <pre><code>$.ajax( { type: "POST", url: "@Url.Action("ReorderClues", "Clues", Model)", data: JSON.stringify(positions), contentType:'application/json' }); </code></pre> <p>And my controller signature is as follows: </p> <pre><code>public ActionResult ReorderClues(TreasureHuntDetails treasureHuntDetails, int[] ids) </code></pre> <p>What I find is that when the javascript invokes the controller in this way, the data model (the TreasureHuntDetails object) is populated with only the treasureHuntId, and everything else is null. The second parameter, the ids array populates successfully - this is the one that I use JSON.stringify() on in the ajax function. </p> <p>I can't figure out why everything else is null apart from the treasureHuntId. In the form, I have a HiddenFor line for every property of the object, like this: </p> <pre><code> @Html.HiddenFor(m =&gt; m.CompletionMessage) @Html.HiddenFor(m =&gt; m.Description) @Html.HiddenFor(m =&gt; m.Clues) @Html.HiddenFor(m =&gt; m.Leaderboard) @Html.HiddenFor(m =&gt; m.TreasureHuntId) etc. </code></pre> <p>I don't see what's special about the TreasureHuntId variable that it is the only one whose value is the only populated one on the object which reaches the controller. Any ideas? What could I be doing wrong? </p> <p>When I invoke the controller the normal way with just the data model (TreasureHuntDetails), everything gets populated properly, so it's only in the javascript where I'm also adding on the Ids array (the 'positions' variable in the javascript code snippet is an integer array). </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload