Null incoming id in Asp.Net MVC controller action

Asked

Viewed 179 times

1

I have a list of requests(Vr), in this list I created a button to finalize this request(Open Mv).inserir a descrição da imagem aqui

When I click this button, I am redirected to my Low view, which has its own attributes, plus the Request Id (Vr). This part is working all right, I’m able to click the button, and load the selected object in the downtown view. The problem is in [httpPost], When I click submit, the Request Id is null, and the Low Id comes with the Request Id.

Action get

public ActionResult MvVr(int id)
{
     Vr vrs = new Vr();
     vrs = vrs.BuscarPorIdDaVr(id);
     Mv mv = new Mv();
     mv.Vrs = vrs;
     return View(mv);
}

Action view Only The part that loads the request data

@model Systemsgarage.Models.Mv

Panel of Registration

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

            <div class="form-horizontal">

                <hr />
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                @Html.HiddenFor(model => model.Vrs.VrId, "VrId")

                <fieldset class="well">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="col-md-4">
                                <div class="form-group">
                                    @Html.LabelForModel("Motorista", htmlAttributes: new { @class = "" })

                                    @Html.DisplayFor(model => model.Vrs.Motorista.Nome, new { htmlAttributes = new { @class = "form-control" } })


                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    @Html.LabelForModel("Usuário", htmlAttributes: new { @class = "" })

                                    @Html.DisplayFor(model => model.Vrs.Usuario.Nome, new { htmlAttributes = new { @class = "form-control" } })


                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    @Html.LabelForModel("Itinerario", htmlAttributes: new { @class = "" })

                                    @Html.DisplayFor(model => model.Vrs.Itinerario, new { htmlAttributes = new { @class = "form-control" } })

                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    @Html.LabelForModel("VrId", htmlAttributes: new { @class = "" })

                                    @Html.DisplayFor(model => model.Vrs.VrId, new { htmlAttributes = new { @class = "form-control" } })

                                </div>
                            </div>


                        </div>
                    </div>

Action Httppost

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult MvVr(Mv objMv, int VrId)
{
     if (ModelState.IsValid)
     {
         var identity = User.Identity as ClaimsIdentity;
         var login = identity.Claims.FirstOrDefault(c => c.Type == "Id").Value;
         var usuario = db.UsuariosDb.FirstOrDefault(u => u.Id.ToString() == login);
          objMv.IdUsuario = usuario.Id; // Usuário que Finalizou a Vr


          //objMv.VrId = VrId;
          db.MvDb.Add(objMv);
          db.SaveChanges();
          TempData["Sucesso"] = "Baixa Realizada Com Sucesso.";
          return RedirectToAction("Index", "Painel");
      }
      TempData["Errado"] = "Favor Preencher os Campos Corretamente.";
      return View(objMv);
}
  • @Html.HiddenFor(model => model.Vrs.VrId, "VrId") is generating an html, open this page in the browser right-click display source code and check how the Hidden input name was generated and paste here.

  • <input length="4" data-val="true" data-val-number="The Vrid field must be a number." data-val-required="The Vrid field is required." id="Vrs_vrid" name="Vrs.Vrid" value="11" type="Hidden">

  • the Name is Vrs.VrId and not VrId in the controller understood the problem?

  • Put a line like this @Html.Hidden("VrId", model.Vrs.VrId) will work

  • @Html.Hidden("Vrid", model.Vrs.Vrid) Like this build error.

  • <input data-val="true" data-val-number="The Vrid field must be a number." data-val-required="The Vrid field is required." id="Vrid" name="Vrid" value="0" type="Hidden"> "

  • @Html.Hidden("Vrid", model.Vrs.Vrid) - Sorry, I was stupid, that way it worked, I was putting "Hiddenfor". Only that the MV Id(Low) tbm is coming with the value of the Vr Id(Request)

  • it is complicated to say without having to debug but, if it is changed the field of the value is only you verifying what is the value that has to put there inside...

Show 3 more comments
No answers

Browser other questions tagged

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