使用ViewModel将数据从视图发送到模型

时间:2015-07-07 08:33:42

标签: c# asp.net-mvc razor

我有这个ViewModel:

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

里面有两个模型,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; }
}

和RefAnomalie。

但是,当我想从一个表单将视图中的数据发送到控制器时,我不断收到异常。 观点:

@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")
  }

然后是控制器:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我认为您遇到了问题,因为您的模型RapportAnomalie中没有初始化lignesRapport字段

创建构造函数并初始化lignesRapport。我相信这个问题会消失。

    public RapportAnomalie()
    {
         lignesRapport = new List <LigneRapportAnomalie>();
    }
祝你好运