简体   繁体   中英

Pass lists of selected relevant hidden field values form to the controller

Can anyone please tell me how do I pass lists of selected relevant hidden field values from to controller. The assigned field return value is Boolean(true). There are over 100 list items. If admin person select(Assigned) list and submit, it'll pass all list values pass to the controller and update database.

This my view page chtml code:

@using (Html.BeginForm("CAssigned", "Orders", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @foreach (var item in Model)
    {
        @Html.HiddenFor(model => item.LocationId)
        <td>
            @Html.DisplayFor(modelItem => item.MapIcons)
            @Html.HiddenFor(model => item.MapIcons,"MapIcons")
        </td>
        <td>
            @Html.EditorFor(modelItem => item.Assigned)
            @Html.HiddenFor(model => item.Assigned)
        </td>
    } 
    <input type="Submit" value="Select" />
}

My controller

   [HttpGet]
    public ActionResult CAssigned()
    {
        var orders = db.Locations.ToList();
        return View(orders);
    }

    [HttpPost] 
   public ActionResult CAssigned(IEnumerable<Location>) 
    {
    return View();
    }

html view source

<input data-val="true" data-val-number="The field LocationId must be a number." data-val-required="The LocationId field is required." id="item_LocationId" name="item.LocationId" type="hidden" value="1" />
<td>
    red-dot.png
    <input Length="8" id="item_MapIcons" name="item.MapIcons" type="hidden" value="red-dot.png" /></td>
<td>
<input class="check-box" data-val="true" data-val-required="The Assigned to Courier field is required." id="item_Assigned" name="item.Assigned" type="checkbox" value="true" /><input name="item.Assigned" type="hidden" value="false" />
<input type="Submit" value="Select" />
<input id="item_Assigned" name="item.Assigned" type="hidden" value="False" />
<td>

Your use of a foreach loop does not generate the correct name attributes necessary to bind to a collection and you need to use a for loop or a custom EditorTemplate for typeof Location so that the form controls include an indexer. Refer this answer for more details on how form controls for a collection need to be named, and for using an EditorTemplate .

To use a for loop, you view needs to be

@model IList<yourAssembly.Location>
@using (Html.BeginForm()) // no need to add the parameters since you posting to the same method
{
  <table>
    for(int i = 0; i < Model.Count; i++)
    {
      <tr>
        <td>
          @Html.HiddenFor(m => m[i].LocationId)
          @Html.HiddenFor(m => m[i].MapIcons)
          @Html.DisplayFor(m => m[i].MapIcons)
        </td>
        <td>
          @Html.CheckBoxFor(m => m[i].Assigned)
        </td>
      </tr>
    }
  </table>
  <input type="Submit" value="Select" />
}

and you post method signature needs to be (add a name for the parameter)

[HttpPost] 
public ActionResult CAssigned(IEnumerable<Location> model)

Side notes:

  1. Remove the hidden input for the Assigned property. You have already generated a checkbox (and associated hidden input) for the property so it would just be ignore on postback anyway
  2. An <input> is not a valid child of a <tr> element, so make sure you include them inside a <td> element

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM