0

I have three related tables

Tables

In the table "SelectedMaterial" need to enter the values in the column "CountMaterial" (initially there null)

Values

For this in controller I have POST and GET methods

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");
    }

I also have a view

@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>
}

But from the view comes null values. How to pass list from View with a value?

nlipatov
  • 53
  • 1
  • 10

1 Answers1

0

Something like Below. The Posted back values have to be in array form.

  @using (Html.BeginForm())
  {
    foreach (var item in Model)
    {
     var count= 0;
     @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", @name="SelectedMaterial[" + count.ToString() + "].CountMaterial"} })
              @Html.ValidationMessageFor(model => item.CountMaterial, "", new { @class = "text-danger" })
         </div>
      </div>
     count+=1;
     }

   }
Scott Dobbins
  • 294
  • 1
  • 8