Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using ajax you are posting only the li's. I see two options, post the entire form using ajax or setup a handler so that on submit it copies the li data to a hidden.</p> <p><b>Update:</b> if you want to post the whole for using ajax you can do something like</p> <pre><code>function postSortOrder() { $.ajax({ data: { selectedCountries: $('#OperationCountries li').get(), id: $('#id') }, type: 'POST' }); } </code></pre> <p><b>Update 2:</b> mvc can handle arrays of items also. Model:</p> <pre><code>public class EditCountriesViewModel { public string Note { get; set; } public int id { get; set; } public Country[] Countries { get; set; } } public class Country { public string Name { get; set; } public int Id { get; set; } public bool Selected { get; set; } } </code></pre> <p>Controller:</p> <pre><code>[HttpPost] public ActionResult EditCountries(EditCountriesViewModel countries) </code></pre> <p>View:</p> <pre><code>@using (Html.BeginForm()) { @Html.LabelFor(model =&gt; model.id); @Html.EditorFor(model =&gt; model.id); @Html.LabelFor(model =&gt; model.Note); @Html.EditorFor(model =&gt; model.Note); &lt;ul&gt; @for(var i = 0; i &lt; Model.Countries.Count(); i++) { &lt;li class="country"&gt;@Model.Countries[i].Name @Html.CheckBoxFor(model =&gt; model.Countries[i].Selected)&lt;/li&gt; @Html.HiddenFor(model =&gt; model.Countries[i].Id) } &lt;/ul&gt; &lt;input type="submit" /&gt; } </code></pre> <p><b>Update 3:</b></p> <pre><code>function onSubmitHandler() { $("#hidden_selectedCountriesString").val($('#OperationCountries li').get()); } </code></pre> <p>then you would need to split the result to get the numbers back into an array.</p> <p><b>Update 4:</b> Controller:</p> <pre><code>[HttpPost] public ActionResult EditCountries(int id, string name, int[] enabledCountries) </code></pre> <p>View: </p> <pre><code>@using (Html.BeginForm()) { @Html.Label("id"); @Html.TextBox("id"); @Html.Label("name"); @Html.TextBox("name"); &lt;ul&gt; @foreach (var country in countries) { &lt;li class="country"&gt;@country.Value &lt;input type="checkbox" name="enabledCountries", value = "@country.Key" /&gt;&lt;/li&gt; } &lt;/ul&gt; &lt;input type="submit" /&gt; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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