Html.BeginForm () Do not submit model, ModelState.IsValid = False

3

Next I'm with the Html.BeginForm () where the same submits the model but when it goes to the same control it finds the ModelState.IsValid false. Follows Template, Control and BeginForm Model:

[Table("TB_Estado")]
public class Estado
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    public string sigla { get; set; }
    public string estado { get; set; }
}

Control [Post] Create

   [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Estado estado)
    {
        if (ModelState.IsValid)
        {
            db.Estados.Add(estado);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(estado);
    }

View Create

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

    <div class="form-horizontal">
        <h4>Estado</h4>
        <hr />
        <div class="form-group">
            @Html.LabelFor(model => model.sigla, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.sigla, new { htmlAttributes = new { @class = "form-control" } })
            </div>
        </div>

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

See the image that illustrates the problem:

AsrequestedbythepersonneltheinspectionoftheKeysandValuesofModelState,hadalreadyanalyzedanditsaysthatitwasnotpossibletoconverttheString,beingthatthefieldsareallStringandsimple,theIDisautoincrement.I'mstartingnowinASP.NETMVCandapologizeforanyNubisse,lol.

Errormessage="The parameter conversion from type 'System.String' to type 'pedicare.sidigital.com.br.web.Models.Estado' failed because no type to convert between these types. ' >

    
asked by anonymous 09.12.2015 / 20:59

1 answer

3

I found the problem in this case. I have noticed that in the signature of the% Create(Estado estado) method the State object has the same name as the variable of the state class "state", which confuses the ModelBinder . In this case, changing the name of the internal variable, the problem has been solved.

14.12.2015 / 23:26