so what Iam trying to do is a view wiht both create and list in the same view, so I've read that I can usea ViewModel for this so I create my view model "EventoViewModel"
public class EventoViewModel
{
public Eventos Eventos { get; set; }
public IEnumerable<Eventos> LEventos { get; set; }
}
my controller:
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Eventos eventos)
{
if (ModelState.IsValid)
{
db.Eventos.AddObject(eventos);
db.SaveChanges();
}
return RedirectToAction("Create");
}
My view: in the foreach part I get an ERROR, say nullreferenceException, what Iam doing wrong?
@model createList.Models.EventoViewModel
<h2>Create</h2>
@using (Html.BeginForm()) {
<fieldset>
<div class="editor-label">
@Html.LabelFor(model => model.Eventos.Nombre)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Eventos.Nombre)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<table>
<tr>
<th>
Nombre
</th>
</tr>
@foreach (var item in Model.LEventos) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Nombre)
</td>
</tr>
}
</table>