Doubt with Routes in Asp.net mvc

1

When you create a Controller , you create one:

public ActionResult Index()
    {
        return View();
    }

But in this Controller , if you have a new Action and a parameter is necessary, if the user tries to open this link it will generate an error. Is there any way to avoid this error when View is sending a parameter like this:

foreach (var item in Model)
    {
        <a href="/Relatorios/ListaSelecionado/@item.IDJOGOCAIXA" class="list-group-item">
           Número :  @Html.DisplayFor(c => item.IDJOGOCAIXA) - 
        </a>

    }


public ActionResult ListaSelecionado(int id)
   {
        return View();
   }

Error:

    
asked by anonymous 29.07.2016 / 16:55

1 answer

2
  

But in this Controller, if you have a new Action with a parameter needed, if the user tries to open this link it will generate an error. Is there any way to avoid this error?

I do not understand what you mean by this, but if you do not send the parameter to Action , it will give you the same error.

If we're talking about a method like this:

public ActionResult ListaSelecionado(int id) { ... }

A route is expected like this:

http://teste:12345/MeuController/ListaSelecionado/1

This considering that your method is GET . If it is POST , id is only filled if the submission is made from a form that contains a <input> called id , or the request has a field named id in the data body.

EDIT

Note that the error you're having clearly states that Action is requesting an integer parameter called id in the route. So, access:

http://teste:12345/MeuController/ListaSelecionado/

It will give error, because id is not filled. Now, if you pass any number after ListaSelecionado/ :

http://teste:12345/MeuController/ListaSelecionado/234

It will work, however id does not exist.

    
29.07.2016 / 17:13