Omar Chaabouni
Omar Chaabouni

Reputation: 446

Send Data From View to Model With ViewModel

I have this ViewModel:

 public class Rapport
{
    [Key]
    public int RapportId { get; set; }
    public RapportAnomalie rapport { get; set; }
    public IEnumerable<RefAnomalie> refAnomalies { get; set; }
}

which has two models in it, RapportAnomalie :

 public class RapportAnomalie
{
    [Key]
    public int codeRapport { get; set; }
    public DateTime date { get; set; }
    public String heure { get; set; }
    public String etat { get; set; }

    [ForeignKey("codeAgence")]
    public virtual Agence agence { get; set; }
    public int codeAgence { get; set; }

    public IEnumerable<LigneRapportAnomalie> lignesRapport { get; set; }
}

and RefAnomalie.

However when I want to send data from view to controller from a form, I keep getting an exception. The view :

@model InspectionBanque.Models.Rapport

@{
    ViewBag.Title = "Create";
 }

  <h2>Create</h2>
   @using (Html.BeginForm()) 
   {
      @Html.AntiForgeryToken()
    <div class="form-horizontal">
    <h4>RapportAnomalie</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.rapport.date, htmlAttributes: new {               @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.rapport.date, new { htmlAttributes = new { @class = "form-control" } })     
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.rapport.heure, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.rapport.heure, new { htmlAttributes = new { @class = "form-control" } })               
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.rapport.etat, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.rapport.etat, new { htmlAttributes = new { @class = "form-control" } })

        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.rapport.codeAgence, "codeAgence", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("codeAgence", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.rapport.codeAgence, "", new { @class = "text-danger" })
        </div>
    </div>          
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    }
      @for (int i = 0; i < Model.refAnomalies.Count(); i++)
     { 
       <div class="col-md-10">
     @Html.DisplayFor(model => model.refAnomalies.ElementAt(i).libele)      
            </div>
  }
   <div>
    @Html.ActionLink("Back to List", "Index")
   </div>

   @section Scripts {
     @Scripts.Render("~/bundles/jqueryval")
  }

and then the controller :

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create( Rapport rapportAnomalie)
    {
        if (ModelState.IsValid)
        {
            RapportAnomalie rp = new RapportAnomalie();

            db.rapportAnomalies.Add(rapportAnomalie.rapport);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        var refanomal = from r in db.refAnnomalies
                        select r;

    Rapport rapport = new Rapport { rapport = rapportAnomalie.rapport, refAnomalies = refanomal.ToArray() };

        ViewBag.codeAgence = new SelectList(db.Agences, "codeAgence", "intituleAgence", rapportAnomalie.rapport.codeAgence);
        return View(rapport);
    }

Any ideas what's wrong with it?

Upvotes: 0

Views: 254

Answers (1)

alikuli
alikuli

Reputation: 546

I think you are getting the problem because the lignesRapport field is not initialized in your model RapportAnomalie

Create a constructor and initialize lignesRapport . I believe the problem will go away.

    public RapportAnomalie()
    {
         lignesRapport = new List <LigneRapportAnomalie>();
    }

Good luck

Upvotes: 0

Related Questions