Reputation: 57
Hello I have a problem when running my app I have an error like this:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[SR.Models.Incydent]', but this dictionary requires a model item of type 'SR.Models.Incydent'.
I know that it's caused because in view may be bad reference to model. But my action looks like:\
[HttpGet]
public ActionResult ZglosIncydent()
{
return View();
}
[HttpPost]
public ActionResult ZglosIncydent(Incydent incydent)
{
if (ModelState.IsValid)
{
incydent.Id = incydent.generateID();
incydent.DateAdded = DateTime.Now;
incydent.Status = "NO";
incydent.Localization = "{52.1234, 53.1234}";
incydent.Phone = "+48159623478";
dbIncydent.Incydents.Add(incydent);
dbIncydent.SaveChanges();
setFlash("Zgłoszenie Przyjęte", MyController.SUCCESS);
return RedirectToAction("Index", "Index", new { Area = "SR" });
}
return View(incydent);
}
And in my View:
@model SR.Models.Incydent
@{
ViewBag.Title = "Zgłoś Incydent";
Layout = "~/Areas/SR/Views/Shared/_SRLayout.cshtml";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken();
@Html.ValidationSummary(true);
<div class="form-group">
<div class="input-group" style="width: 50%;">
<label for="Type">Typ Incydentu</label>
<select class="form-control" id="Type" name="Type" required="required" style="width: 100%">
<option value="WS">Wypadek Samochodowy</option>
<option value="P">Pożar</option>
<option value="Z">Zasłabnięcie</option>
</select>
</div>
</div>
<div class="form-group">
<div class="input-group" style="width: 50%;">
<label for="Description">Opis</label>
<textarea id="Description" class="form-control" name="Description" required="required" style="resize: none;" rows="5"></textarea>
</div>
</div>
<input type="submit" value="Zgłoś Incydent" class="btn btn-default" />
}
When I saw the error I thought that line with @model cause the problem but it has a proper type. In controller I have second action View which return IENumerable and it's work fine.
Below View and Index action.
public ActionResult Index()
{
return RedirectToAction("Index", "Index", new { Area = "SR" });
}
[HttpGet]
[Authorize(Roles = "Koordynator")]
public ActionResult View(String Type = "", String Status = "", String Order = "")
{
var Incydents = from s in dbIncydent.Incydents select s;
switch (Order)
{
case "id_desc":
Incydents = Incydents.OrderByDescending(s => s.Id);
break;
case "id_asc":
Incydents = Incydents.OrderBy(s => s.Id);
break;
case "date_desc":
Incydents = Incydents.OrderByDescending(s => s.DateAdded);
break;
case "date_asc":
Incydents = Incydents.OrderBy(s => s.DateAdded);
break;
case "type_desc":
Incydents = Incydents.OrderByDescending(s => s.Type);
break;
case "type_asc":
Incydents = Incydents.OrderBy(s => s.Type);
break;
case "status_desc":
Incydents = Incydents.OrderByDescending(s => s.Status);
break;
case "status_asc":
Incydents = Incydents.OrderBy(s => s.Status);
break;
default:
Incydents = Incydents.OrderBy(s => s.DateAdded);
break;
}
if (Type != "all" && Status != "all")
Incydents = Incydents.Where(s => s.Status.Contains(Status) && s.Type.Contains(Type));
else if (Type != "all" && Status == "all")
Incydents = Incydents.Where(s => s.Type.Contains(Type));
else if (Type == "all" && Status != "all")
Incydents = Incydents.Where(s => s.Status.Contains(Status));
return View(Incydents.ToList());
}
Upvotes: 0
Views: 68
Reputation: 20033
Change
@using (Html.BeginForm())
to
@using (Html.BeginForm("ActionNameHere", "ControllerNameHere", FormMethod.Post)
So that the form
knows where to POST
it's data.
Upvotes: 0