0

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:

Genereted HTML

11
  • Which line is throwing the error? Commented Nov 2, 2015 at 16:47
  • ViewBag.tipoImovelID = new SelectList(db.TipoImovel, "ID", "tipoImovel", TipoImovel.tipoImovelID); This one but it shouldn't even get there. It should enter the IF clause. Commented Nov 2, 2015 at 16:49
  • the entire parameter is null in in the POST, or the properties on it? Commented Nov 2, 2015 at 16:50
  • 1
    Where is db.TipoImovel defined in the Create GET method? Commented Nov 2, 2015 at 16:50
  • 1
    This may be related to the way your param is named. You have a class named TipoImovel and an action parameter named TipoImovel. 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. Commented Nov 2, 2015 at 16:52

1 Answer 1

1

Your model has a property string TipoImovel but you have also named the parameter of your POST method TipoImovel (and even more confusing, your class is also named TipoImovel)

Change the name of the parameter so that it dos not match one of the properties of your model, say

public ActionResult Create(TipoImovel model)
{
    ....
}
Sign up to request clarification or add additional context in comments.

1 Comment

I love you. And it was such a simple thing. I literally changed it to model and it worked. Small things... Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.