簡體   English   中英

另一個null集合傳遞給MVC Controller

[英]Another null collection being passed to MVC Controller

我需要其他眼睛才能看到:

  1. 當我嘗試將對象集合傳遞給MVC控制器時,我做錯了,而我得到的只是sgList = null。
  2. 如何檢查以便僅保存要更改的行。

     [HttpPost] public ActionResult Index(IList<EZone_ServiceGroup> sgList) { try { foreach (EZone_ServiceGroup sg in sgList) svcGroupRepo.UpdateServiceGroup(sg); return RedirectToAction("Index"); } catch { return View(); } } 

視圖:

@model IEnumerable<KTCEzone.Domain.Entities.EZone_ServiceGroup>
@{
    ViewBag.Title = "Index";
}
@using (Html.BeginForm())
{
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
    <div class="row">
        <table class="table table-condensed table-bordered table-hover table-striped small" id="sgTable">
            <tr>
                <th class="col-sm-12">@Html.DisplayNameFor(model => model.GroupID)</th>
                <th>@Html.DisplayNameFor(model => model.GroupName)</th>
                <th>@Html.DisplayNameFor(model => model.ParentGroupID)</th>
                <th>@Html.DisplayNameFor(model => model.Active)</th>
                <th>@Html.DisplayNameFor(model => model.OrderIndex)</th>
            </tr>

            @{var items = Model.ToArray();}
            @for (int i = 0; i < items.Length; i++)
            {
                <tr>
                    <td>@Html.DisplayFor(modelItem => items[i].GroupID)</td>
                    <td>@Html.EditorFor(modelItem => items[i].GroupName) </td>
                    <td>@Html.EditorFor(modelItem => items[i].ParentGroupID) </td>
                    <td>@Html.CheckBoxFor(modelItem => items[i].Active) </td>
                    <td>@Html.EditorFor(modelItem => items[i].OrderIndex) </td>
                </tr>
            }
        </table>
    </div>
}

模型:

public class EZone_ServiceGroup
{
    public int GroupID { get; set; }
    public string GroupName { get; set; }
    public bool Active { get; set; }
    public int OrderIndex { get; set; }
    public int ParentGroupID { get; set; }
}

將模型更改為@model IList<KTCEzone.Domain.Entities.EZone_ServiceGroup> ,然后從視圖中刪除@{var items = Model.ToArray();}並使用

@for (int i = 0; i < Model.Count; i++)
{
  <tr>
    <td>@Html.DisplayFor(m => m[i].GroupID)</td>
    <td>@Html.EditorFor(m=> m[i].GroupName)</td>
    <td>@Html.EditorFor(m=> m[i].ParentGroupID)</td>
    <td>@Html.CheckBoxFor(m=> m[i].Active) </td>
    <td>@Html.EditorFor(m=> m[i].OrderIndex) </td>
  </tr>
}

它將正確命名您的元素。 如果無法將集合更改為IList,則需要使用自定義EditorTemplate作為模型的類型,並與@Html.EditorFor()結合使用

至於“如何檢查以便只保存被更改的行” ,所有控件都將被回發,因此您需要將已發布的值與控制器中的原始值進行比較。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM