0

これは私のアクションビューです:

public ActionResult promo()
{
    var model = (from p in entity.promotion
                 where p.vehicule.Idag == User.Identity.Name
                 select p).ToList();
    return View(model);
}

これは私の HttpPost アクションです:

[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();
    }
} 

アクションを呼び出すと、次のエラーが発生します。

La référence d'objet n'est pas définie à une instance d'un object.

この行で:

<% foreach (var item in Model) { %>

URLに移動してgoキーをクリックするとこのエラーが発生した後でも、ページ間を移動するとページが正常に機能します。ここに何か単純なものが欠けていると思いますか?

4

1 に答える 1

1

ビューにはモデルが必要なようです。最初の例では、次の適切なモデルを与えています。

return View(model);

2 番目のケースでは、どのブランチにもモデルを渡していません。

return View();

舞台裏では、foreach ループを反復処理すると、Model.GetEnumerator() が呼び出され、モデルが null の場合、表示されている null 参照例外が発生します。モデルが null でないことを確認する必要があります。Enumerable.Empty<T>();

return View(System.Linq.Enumerable.Empty<PromotionType>());
于 2012-05-06T22:08:06.957 に答える