如何获得List<〜>从视图到控制器MVC

时间:2016-12-12 19:04:47

标签: asp.net-mvc

我有三个相关的表

Tables

在表格#34; SelectedMaterial"需要输入列中的值" CountMaterial" (最初有空)

Values

对于控制器中的这个,我有POST和GET方法

public ActionResult EditCountOfMaterial(int id)
    {
        var selectedmaterial = db.SelectedMaterials.Where(m => m.CardId == id).ToList();
        return View(selectedmaterial);
    }

    [HttpPost]
    public ActionResult EditCountOfMaterial(List<SelectedMaterial> material)
    {
        //material = db.SelectedMaterials.Where(m => m.CardId == 3).ToList();

        for (int i = 0; i < material.Count; i++)
        {
            //material[i] = db.SelectedMaterials.Where(m => m.CardId == 3).First();
            db.Entry(material[i]).State = EntityState.Modified;               
            db.SaveChanges();
        }
        return RedirectToAction("Index");
    }

我也有一个观点

@model IEnumerable<AccountingPlusProject.Models.SelectedMaterial>
@{
ViewBag.Title = "EditCountOfMaterial";
}

<h2>EditCountOfMaterial</h2>
@using (Html.BeginForm())
{
foreach (var item in Model)
{
    @item.ReferenceMaterial.NameMaterial
    <div class="form-group">
        @Html.LabelFor(model => item.CountMaterial, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => item.CountMaterial, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => item.CountMaterial, "", new { @class = "text-danger" })
        </div>
    </div>
}
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Save" class="btn btn-default" />
    </div>
</div>
}

但是从视图中出现了空值。如何从View传递带有值的列表?

1 个答案:

答案 0 :(得分:0)

像下面这样的东西。已发布的返回值必须采用数组形式。

myContent