ID is null when I enter the details by index

Asked

Viewed 46 times

2

When I enter the registration details by the ID index it is always null.

http://localhost:17542/Availability/Details/0

Controller Details:

public ActionResult Detalhes(int? id)
        {
            var disponibilidade = db.Disponibilidade.Where(d => d.DisponibilidadeID == id).FirstOrDefault();

            if (disponibilidade == null)
            {
                return new HttpNotFoundResult();
            }
            DisponibilidadeViewModel model = new DisponibilidadeViewModel()
            {     
                AnoPastoral = disponibilidade.AnoPastoral,
                DiaDisponivel = disponibilidade.DiaDisponivel,
                HoraDisponivel = disponibilidade.HoraDisponivel,
                Observacoes = disponibilidade.Observacoes
            }; 
            return View(model);
        }

Create controller:

public ActionResult CriarDisponibilidade([Bind(Include = "DisponibilidadeID,AnoPastoral,DiaDisponivel,HoraDisponivel,Observacoes,CatequistaID")] Disponibilidade disponibilidade)
        {
            if (ModelState.IsValid)
            {
                db.Disponibilidade.Add(disponibilidade);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(disponibilidade);
        }

Action Index:

@model IEnumerable<WebAppCatechesis2.ViewModels.DisponibilidadeViewModel>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_mytemplate.cshtml";
}

<div id="page-wrapper">
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">Lista Disponibilidades de cada catequista</h1>
        </div>
        <!-- /.col-lg-12 -->
    </div>
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Futuramente vai ter aqui opções
                </div>
                <div class="panel-body">
                    <div class="table-responsive">
                        <table class="table table-striped table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th class="info">
                                        Nome do Catequista
                                    </th>
                                    <th class="info">
                                        AnoPastoral
                                    </th>
                                    <th class="info">
                                        DiaDisponivel
                                    </th>
                                    <th class="info">
                                        HoraDisponivel
                                    </th>
                                    <th class="info">
                                        Observacoes
                                    </th>
                                    <th class="info">
                                        Opções
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var item in Model)
                                {
                                    <tr>
                                        <td>
                                            @Html.DisplayFor(model => item.Nome)
                                        </td>
                                        <td>
                                            @Html.DisplayFor(model => item.AnoPastoral)
                                        </td>
                                        <td>
                                            @Html.DisplayFor(model => item.DiaDisponivel)
                                        </td>
                                        <td>
                                            @Html.DisplayFor(model => item.HoraDisponivel)
                                        </td>
                                        <td>
                                            @Html.DisplayFor(model => item.Observacoes)
                                        </td>
                                        <td>
                                            @Html.ActionLink("Editar", "Editar", new { id = item.DisponibilidadeID }) |
                                            @Html.ActionLink("Detalhes", "Detalhes", new { id = item.DisponibilidadeID }) |
                                            @Html.ActionLink("Apagar", "Apagar", new { id = item.DisponibilidadeID })
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                    <!-- /.table-responsive -->
                </div>
                <!-- /.panel-body -->
            </div>
            <!-- /.panel -->
        </div>
        <!-- /.col-lg-6 -->
    </div>
</div>                                     

Viewmodel:

 public class DisponibilidadeViewModel
    {
        public int DisponibilidadeID { get; set; }
        public String Nome { get; set; }
        public String AnoPastoral { get; set; }
        public String DiaDisponivel { get; set; }
        public String HoraDisponivel { get; set; }
        public String Observacoes { get; set; }
    }
  • You can put in your question how you are doing to call this URL?

  • I have already added the amendment

  • The link is called a View associated with DisponibilidadeController? You should not go ID 0. Can you tell me why the link is being generated with this ID 0?

  • If the id is manually placed in the url it works perfectly. But I don’t understand why it is 0.

  • Yes, the problem is obviously the link. You can put in your question to Action that saves an availability?

  • My availabilities are being stored correctly. With the correct Ids.I will put the complete View.

  • On top is the action of jumps an availability.

Show 3 more comments

2 answers

3


If you’re wearing one Viewmodel for their Views, need to fill in DisponibilidadeID, otherwise it obviously won’t work:

        DisponibilidadeViewModel model = new DisponibilidadeViewModel()
        {   
            DisponibilidadeID = disponibilidade.DisponibilidadeID,
            AnoPastoral = disponibilidade.AnoPastoral,
            DiaDisponivel = disponibilidade.DiaDisponivel,
            HoraDisponivel = disponibilidade.HoraDisponivel,
            Observacoes = disponibilidade.Observacoes
        }; 

Controller problem Index:

Select new DisponibilidadeViewModel { Nome = p.Nome, DisponibilidadeID = d.DisponibilidadeID, AnoPastoral = d.AnoPastoral, DiaDisponivel = d.DiaDisponivel, HoraDisponivel = d.HoraDisponivel, Observacoes = d.Observacoes });

Was missing: DisponibilidadeID = d.DisponibilidadeID

  • Had already done this, the problem is that when I click on the Index Details it does not enter the method "Actionresult Details".

  • I’m gonna need the Action Index in your question too.

  • It was amended with the Action Index, and Model View

  • So that’s it. You’re not filling in DisponibilidadeID, therefore the mistake.

  • How can I fill this in Viewmodel?

  • I have already put this in my answer. It is not yet clear?

  • DisponibilidadeID = disponibilidade.DisponibilidadeID,, this field is only filled in if you place the ID in the URL.

  • var disponibilidade = db.Disponibilidade.Where(d => d.DisponibilidadeID == id).FirstOrDefault();,is the NULL and I can’t get around this.

  • I don’t think you understand. The wrong fill is in Index. Not in Details. In Details it’s all right.

  • Sorry. OK I’ve figured out my problem.

Show 5 more comments

0

In my experience I can tell you the following.

If you change the URL to :

http://localhost:17542/Availability/Details? id=0

It’ll work the way it is...

Now, to make this question URL work just put the following line in the file Route.Config.cs

        routes.MapMvcAttributeRoutes();

Would look like this:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapMvcAttributeRoutes();


            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

And put the following annotation in your method:

    [Route("seucontrole/detalhes/{id}")]

With that the route with /0 works.

  • I think my problem is not with Routes, but with the ID, which is not injected into the method.

  • Run your project and run the URL as I put it in the answer, the value goes right. Do the test for you to see. Without changing anything, just call the URL that way

  • I have tested and it didn’t work, but if putting the ID manually in the URL works perfectly.

  • What is manually, what would change in this url manually to work? http://localhost:17542/Availability/Details? id=0

  • Putting the ID in the URL works perfectly

  • So, that’s what I said... If you do what I said in the answer your route (Original of the question) will work...

  • It doesn’t work. I think I did everything right

Show 2 more comments

Browser other questions tagged

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