This is my action view :
public ActionResult promo()
{
var model = (from p in entity.promotion
where p.vehicule.Idag == User.Identity.Name
select p).ToList();
return View(model);
}
This is my HttpPost Action :
[HttpPost]
public ActionResult promo(string idv, string dd, string df, string remise)
{
try
{
//some code
ViewData["Resultat"] = "L'ajout de promotion à reussi";
return View();
}
else
{
ViewData["Resultat"] = "Une promotion existe deja dans cette periode";
return View();
}
}
catch (Exception)
{
ViewData["Resultat"] = "L'ajout de promotion à echoué Veillez verifiez le Matricule de véhicule ou ressayer plus tard ";
return View();
}
}
when i call my action i gét this error :
La référence d’objet n’est pas définie à une instance d’un objet.
at this line :
<% foreach (var item in Model) { %>
my page works fine when i navigate between page, even after getting this error when i go to the url and click on go keys it work’s.
I think something simple is missing here?
It looks like your view requires a model. In the first instance you are giving it a proper model with:
In the second case you are not passing a model on any branch:
Behind the scenes, when you iterate over a foreach loop, Model.GetEnumerator() is called, and if model is null, you get the null reference exception that you are seeing. You need to make sure that the model is not null, and you can easily do so using
Enumerable.Empty<T>();