Pass ID to Controller

Asked

Viewed 265 times

1

I have the problem that when I click on the Register button, the City does not pass the ID. I did. The ID is being passed to the screen, but when passing to the controller is not.

<div class="alinhado col-md-4">
    <div class="form-group">
        <div class=" alinhadoLabel">
            @Html.Label("Cidade", htmlAttributes: new { @class = "control-label" })
        </div>
        <div class="alinhadoEditor">
            <select class="form-control" id="City"><option value="0">Selecione</option></select>
        </div>
    </div>
</div>

This is the controller:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "PeopleID,Name,Email,Password,Address,Number,Neighborhood,ZipCode,Photo,Type,Active,Validate,DateBirth,Register,CityID")] People people)
    {
        if (ModelState.IsValid)
        {
            db.Peoples.Add(people);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(people);
    }

and here the error:

inserir a descrição da imagem aqui

1 answer

1


You need to set the name of your select to the same name as the property for bind to occur. In this case, Cityid instead of City

 <select class="form-control" id="CityID" name="CityID"><option value="0">Selecione</option></select>
  • The Name property was really missing, but Cityid didn’t work, it had to be City only

Browser other questions tagged

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