Html.Beginform() No Submits model, Modelstate.Isvalid = False

Asked

Viewed 186 times

3

Next I am with Html.Beginform() where the same Submits the model however when it goes to the control it is false the Modelstate.IsValid. Follows Model, 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 illustrating the problem: inserir a descrição da imagem aqui inserir a descrição da imagem aqui

As requested by the staff the inspection of the Keys and Values of Modelstate, had already analyzed and the same says that it was not possible to convert the String, being that the fields are all String and simple, the ID is auto increment. I am starting now at ASP . NET MVC and I apologize for any Nubisse, rsrsrrsrs.

Error message = "The Parameter Conversion from type 'System.String' to type 'pedicare.sidigital.com.br.web.Models.Status' failed because no type converter can Convert between These types."

inserir a descrição da imagem aqui

  • 2

    the object estado is coming null... it means it is not valid. It is not wrong.

  • 2

    Inspect the ModelState, property Values.

  • 1

    In the same image you posted, if you click on the arrows of > Keys and >Values, he’ll show you exactly what’s going on.

  • What is the @model of his View?

  • First sample of code I put, this is my biggest problem, I don’t understand why not recognize as String.

  • Someone can help this case.

  • Difficult this case. Looking for other means.

Show 2 more comments

1 answer

3

I found the problem with this case. I found that in the signature of the post method Create(Estado estado) the State object has the same name as the variable of the "state" class, which confuses the Modelbinder. In this case, changing the name of the internal variable, the problem was solved.

After solved I found the post below Fabio Gantmans, below the link.

http://www.aprendadotnet.com.br/Artigo/1094/asp-net-mvc-5-model-binder-null-no-httppost

Browser other questions tagged

You are not signed in. Login or sign up in order to post.