I have a model like this:
public class TRViewModel
{
public List<TR> TRecord { get; set; }
}
[Table("TRecord")]
public partial class TRecord
{
[Key]
[DisplayName("TID")]
public int Tid { get; set; }
public String Tname { get; set; }
}
The controller is like this:
public ActionResult Audit(int? id)
{
var tQ = from t in db.TRecordDBSet
select t;
var list = tQ.ToList();
return View(list);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Audit(List<TRecord> list)
{
if (ModelState.IsValid)
{
db.Entry(list).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Audit");
}
return View(list);
}
The view is like this:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Tid)
</th>
<th>
@Html.DisplayNameFor(model => model.Tname)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Tid)
</td>
<td>
@Html.EditFor(modelItem => item.Tname)
</td>
</tr>
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
I want to pass the list to the controller to save the new variable to the database but when debugging I found the list is null
, what is going wrong?