Reputation: 722
I have a list that returnsa list of jobs with a boolean set on false, and in my view I want to change it on true. My problem is that the view is not passing the ID back, I can't see why.
At first I was using foreach but after some research I figured out I should use for with IList.
Here is back-end:
[HttpGet]
[Authorize(Roles = MyRoles.Admin)]
public ActionResult JobsToModerate()
{
var allJobs = db.Jobs.Where(x => x.isModerated == false).ToList();
return View(allJobs);
}
[HttpPost]
[Authorize(Roles = MyRoles.Admin)]
public ActionResult JobsToModerate([Bind(Include = "ID,isModerated")] Job job, EditJobViewModel editJob)
{
if (!ModelState.IsValid)
{
var jobs = db.Jobs.Find(editJob.ID);
if (jobs == null)
{
return HttpNotFound();
}
else
{
jobs.ID = editJob.ID;
jobs.isModerated = editJob.isModerated;
}
try
{
db.SaveChanges();
}
catch (Exception ex) { throw ex; }
}
return View();
}
My View:
@model IList<JobSite3.Models.Job>
@using (Html.BeginForm("JobsToModerate", "Jobs", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
for (var i = 0; i < Model.Count; i++)
{
@Html.HiddenFor(model => model[i].ID)
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model[i].Name)
</th>
<th>
@Html.DisplayNameFor(model => model[i].isModerated)
</th>
<th></th>
</tr>
<tr>
<td>
@Html.DisplayFor(model => model[i].Name)
</td>
<td>
@Html.EditorFor(model => model[i].isModerated)
</td>
</tr>
</table>
<input type="submit" id="btnNew" value="Save" class="btn btn-default" />
}
}
I believe there is a similar problem with solution out there but I haven't found it yet.
Upvotes: 0
Views: 266
Reputation:
The model in your view is IList<Job>
so your POST method needs to match. It needs to be
public ActionResult JobsToModerate(List<Job> model)
{
....
However the code within you method does not make sense for a collection, and its not clear what your EditJobViewModel
class is. I assume you will want to iterate through each Job
and update the database
Upvotes: 1