1

I wish to make a partial view that I could have inside all my other views.

What this partial view does is simple : it puts a new value inside Session["here"] object, then refresh the page.

I could have just put the same form pattern in every view, but i thought it would be more... readable? Optimized ? Anyway, that would be the so called pattern :

@using RatioMVC.Models
@model ChoixPeriodeMod

@{
    SelectList periodes;
    using (SGARDEEntities db = new SGARDEEntities())
    {
        periodes = new SelectList(db.AnneeFinance.ToList(), "id", "Texte");
    }

}

@using (Html.BeginForm("ChoixPeriode", "Tableaux", FormMethod.Post, new { role = "form" }))
{
    <label>Début : </label> @Html.DropDownListFor(t => t.debut, periodes)    <label>Fin : </label> @Html.DropDownListFor(t => t.fin, periodes)
}

The code above is in the partial view, the controller code is below :

    [HttpGet]
    public ActionResult ChoixPeriode()
    {
        ChoixPeriodeMod model = new ChoixPeriodeMod();
        AnneeFinance debut = new AnneeFinance();
        AnneeFinance fin = new AnneeFinance();

        initialise(ref debut, ref fin);

        model.debut = debut;
        model.fin = fin;

        Session["debut"] = model.debut;
        Session["fin"] = model.fin;

        return View(model);
    }



    [HttpPost]
    public ActionResult ChoixPeriode(ChoixPeriodeMod model)
    {
        Session["debut"] = model.debut;
        Session["fin"] = model.fin;

        return View(model);
    }

The error occurs when encountering the partial view line :

@using RatioMVC.Models
@model TotauxMod

@{
ViewBag.Title = "Totaux";
Layout = "~/Views/Shared/_Layout.cshtml";

}



@Html.Partial("ChoixPeriode")

<h2>test view</h2>

The error could be translated as :

A wrong model instance as been passed to the view

I began to wonder : a partial view might not be the best idea.

I would like to have (like a control ascx in web form) a file that handle its own post back. I could put it in every view that requires this short control.

Louis-Roch Tessier
  • 822
  • 1
  • 13
  • 25
Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62

0 Answers0