2

私は次のリストを手に入れました:

public class Items
{
    public String Nro_Item { get; set; }
    public Sucursal Sucursal { get; set; }
    public Areas Area { get; set; }
    public Sectores Sector { get; set; }
    public bool ID_Estado { get; set; }
}

私はそれをこのビューに渡します:

@using ClientDependency.Core.Mvc
@model IEnumerable<TrackingOperaciones.Controllers.Items>

@{
    ViewBag.Title = "Ver Items";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<br/>
@using (Html.BeginForm("CargarRecibidos", "Recepcion", FormMethod.Post))
{    
<table class="table table-striped table-bordered table-condensed">
    <tr>
        <th>
            Numero
        </th>
        <th>
            Sucursal
        </th>
        <th>
            Area
        </th>
        <th>
            Sector
        </th>
    </tr>
  @foreach (var item in Model)
  {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Nro_Item)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Sucursal.descripcion)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Area.descripcion)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Sector.Descripcion)
        </td>
        <td>
            @Html.CheckBoxFor(modelItem => item.ID_Estado)
        </td>
    </tr>
  }
</table>
<p>
    <input class="btn" name="Guardar" type="submit" value="Guardar"/> |
    @Html.ActionLink("Volver al listado de Recepción", "Index")
</p>
}

すべてが正常になるまで:問題は、次のように処理するために、コントローラーでチェックされた値を使用してモデル全体を取得することです。

[HttpPost]
 public ActionResult CargarRecibidos(IEnumerable<Items> items)
        {
            if (ModelState.IsValid)
            {
                //do something here
            }
            return RedirectToAction("Index");
        }
    }

しかし、ポストバックで「アイテム」が空に戻ったので、私は惨めに落ちています

フォームに何か問題があると思います

@using (Html.BeginForm("CargarRecibidos", "Recepcion"))
{  
    <input class="btn" name="Guardar" type="submit" value="Guardar"/>
}

私を助けてください!

4

2 に答える 2

2

IEnumerableを使用する

次に、コントローラーの入力パラメーターはリストになります

おそらく、フォーム送信(よりモダン/エレガント)の代わりにJSONオブジェクトでAJAX POSTを使用することも検討してくださいが、送信も問題ありません。

于 2012-09-25T16:04:54.820 に答える
2

問題の一部は、チェックボックスのみがフォーム値であるため、モデル全体が投稿されないことです。アイテム番号を追跡するためにフォームに非表示の値を追加してから、コントローラーでチェックボックスの値を特別に処理する必要があると思います。アイテム番号は、それらを区別するために、投稿された値の「チェックボックス」に追加されます。

<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Nro_Item)
        @Html.Hidden("Nro_Item", item.Nro_Item)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Sucursal.descripcion)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Area.descripcion)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Sector.Descripcion)
    </td>
    <td>
        @Html.CheckBox("checkbox" + item.Nro_Item, item.ID_Estado)
    </td>
</tr>

コントローラでは、チェックボックスの処理に使用できるように、数値アイテムにのみバインドします。

    [HttpPost]
    public ActionResult CargarRecibidos(List<string> nro_item)
    {

         foreach (string item in nro_item)
         {
            var checkbox=Request.Form["checkbox" + item];
            if (checkbox != "false") // if not false then true,false is returned
            {
                // Do the action for true...
            }
            else 
            {
                // Do the action for false
            }
         }
        return View();
    }
于 2012-09-25T18:31:30.573 に答える