I already saw a bunch of these posts but none helped me because most weren't applied to C# and MVC.
I have the Create for an object named TipoImovel. This object has an auto-generated ID (int), a description (string - tipoImovel) and then the possibly NULL value to a sub-TipoImovel (int? and then the reference TipoImovel). If it's confusing think of it like you create a House (TipoImovel). You can then create a Pool (also TipoImovel) and say it's a sub-type of TipoImovel making it a Pool which is subTipoImovel of House (House with pool). Sorry for the names but they are in my native language. If any questions arise around them please say.
Now here's the code:
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 and POST methods)
// 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>
}
The error comes up because the TipoImovel I receive in the POST method comes as NULL. The form is properly created retreiving any (manually introduced) existing TipoImovel and showing them in the ComboBox but upon hitting "Create" it crashes.
I've been around this problem for 2 days and I can't fix it. Any help is appreciated!
EDIT: Pic of generated HTML:

TipoImoveland an action parameter namedTipoImovel. Your code is probably choking because it's picking the wrong one to reference. Try naming your param in lowercase (i.e.tipoImovel) to remove the abiguity.