对象引用未设置为对象MVC的实例

时间:2015-11-02 16:39:09

标签: c# asp.net-mvc entity-framework

我已经看过很多这些帖子,但没有人帮助我,因为大部分都没有应用于C#和MVC。

我有一个名为TipoImovel的对象的Create。这个对象有一个自动生成的ID(int),一个描述(string - tipoImovel),然后是一个可能的NULL值到一个子TipoImovel(int?然后是参考TipoImovel)。如果它让人困惑,就像你创建一个House(TipoImovel)一样。然后你可以创建一个Pool(也是TipoImovel)并说它是TipoImovel的一个子类型,使它成为一个Pool,它是House的HouseTipoImovel(House with pool)。抱歉这些名字,但他们是我的母语。如果有任何问题,请说出来。

现在这里是代码:

TipoImovel.cs

public class TipoImovel
{

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [Display(Name = "Tipo de Imóvel")]
    [StringLength(20)]
    public string tipoImovel { get; set; }

    [Display(Name = "Sub-Tipo de:")]
    public int? tipoImovelID { get; set; }

    [Display(Name = "Sub-Tipo de:")]
    public virtual TipoImovel subTipoImovel { get; set; }

}

TipoImovelController.cs(GET和POST方法)

// GET: TipoImovel/Create
    public ActionResult Create()
    {
        ViewBag.tipoImovelID = new SelectList(db.TipoImovel, "ID", "tipoImovel");
        return View();
    }

    // POST: TipoImovel/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ID,tipoImovel,tipoImovelID")] TipoImovel TipoImovel)
    {
        if (ModelState.IsValid)
        {
            db.TipoImovel.Add(TipoImovel);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.tipoImovelID = new SelectList(db.TipoImovel, "ID", "tipoImovel", TipoImovel.tipoImovelID);
        return View(TipoImovel);
    }

Create.cshtml

@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>TipoImovel</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.tipoImovel, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.tipoImovel, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.tipoImovel, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.tipoImovelID, "tipoImovelID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("tipoImovelID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.subTipoImovel, "", 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>
}

出现错误,因为我在POST方法中收到的TipoImovel为NULL。正确创建表单,检索任何(手动引入的)现有TipoImovel并在ComboBox中显示它们但是在点击&#34;创建&#34;它崩溃了。

我已经解决了这个问题2天了,我无法修复它。任何帮助表示赞赏!

编辑:生成HTML的图片:

Genereted HTML

1 个答案:

答案 0 :(得分:1)

您的模型有一个属性string TipoImovel,但您还命名了POST方法TipoImovel的参数(更令人困惑的是,您的类也被命名为TipoImovel

更改参数的名称,使其与模型的某个属性不匹配,例如

public ActionResult Create(TipoImovel model)
{
    ....
}