HttpPost上的MVC3 ModelState无效

时间:2012-02-17 18:05:32

标签: asp.net-mvc-3

我遇到的问题是模型在创建HttpGet页面时有效,但在HttpPost上无效(某些属性为null)。

以下是我打开页面的电话:

            var quote = new Quote
            {
                Agency = assignedAgency,
                Insured = insured,
                RiskAddress = insured.Address
            };
            db.Quotes.Add(quote);
            db.SaveChanges();

            return RedirectToAction("Edit", "Quote", quote.QuoteID);  

和HttpGet ActionResult - 此时,模型是有效的。已填充保险和代理商属性,并且还填充了各自的地址属性。

    [HttpGet]
    public ActionResult Edit(int quoteID)
    {
        var model = db.Quotes.Find(quoteID);

       return View(model);
    }

HttpPort ActionResult - 在此方法的条目中,被保险人,代理商及其各自的地址属性为空,导致模型状态无效。

    [HttpPost]
    public ActionResult Edit(Quote model)
    {
        if (ModelState.IsValid)
        {
            if (model.SubCosts > 0)
            {
                model.InsuredSubs = true;
                model.SubClassCode = "95626 - Subcontractor - TEST CLASS";
                model.SubClassExposure = model.SubCosts;
            }

            db.Entry(model).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Edit", new { quoteID = model.QuoteID });
        }
        return View(model);
    }

HTML:

 @model DomaniOnline.Models.DomaniData.Quote

 @{
     ViewBag.Title = "Classification";
  }

  <h2>General Liability Classification</h2>

  @using (Html.BeginForm())
   {
     @Html.ValidationSummary(true)
     <fieldset>

    @Html.HiddenFor(model => model.QuoteID)
    @Html.HiddenFor(model => model.Insured)
    @Html.HiddenFor(model => model.Insured.Address)
    @Html.HiddenFor(model => model.RiskAddress)
    @Html.HiddenFor(model => model.Agency)
    @Html.HiddenFor(model => model.Agency.Address)

        .
        .
        .
        .
    <p>
        @Html.ActionLink("Back to Insured Info", "Edit", "Insured", new { insuredID = Model.Insured.InsuredID }, null)
        <input type="submit" value="Save & Continue" />
    </p>
</fieldset>
}

我想指出页面底部的“返回保险信息”ActionLink工作得很好,能够提供正确的model.Insured.InsuredID。但由于某种原因,在将表单提交给HttpPost时,任何自定义数据类型的属性都为null。 Html.HiddenFor&lt;&gt;在尝试使其工作时添加了测试。

编辑:课程:

 public class Quote
 {
    public virtual int QuoteID { get; set; }
    public virtual Address RiskAddress { get; set; }
    public virtual Agency Agency { get; set; }
    public virtual Insured Insured { get; set; }

    public virtual DateTime PropEffDate { get; set; }
    public virtual bool InsuredSubs { get; set; }
    public virtual int SubCosts { get; set; }
    .
    .
    .
  }
  public class Address
  {
    [Required]
    public virtual string StreetAddress { get; set; }
    [Required]
    public virtual string City { get; set; }
    [Required]
    public virtual string State { get; set; }
    [Required]
    public virtual string ZipCode { get; set; }
  }
  public class Insured
  {
    public virtual int InsuredID { get; set; }
    [Required]
    public virtual string Name { get; set; }
    [Required]
    public virtual Address Address { get; set; }
    public virtual string DBA { get; set; }
    [Required]
    public virtual string BusinessType { get; set; }
    public virtual string Phone { get; set; }
  }

1 个答案:

答案 0 :(得分:1)

您的问题是您在复杂类型上使用HiddenFor。这只知道如何渲染简单类型。

我建议只保留这些对象的ID或使用MVC Futures项目中的Html.Serialize帮助函数序列化您的帐户和保险对象,然后在控制器中反序列化它们。

http://weblogs.asp.net/shijuvarghese/archive/2010/03/06/persisting-model-state-in-asp-net-mvc-using-html-serialize.aspx

保留ID应该只是InsuredID,您可以从中重新获取被保险人及其回复地址。与代理商一样。