Doubt with Routes in Asp.net mvc

Asked

Viewed 31 times

1

When creating a Controller a :

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

But in this Controller, if you have a new Action if a parameter is required, 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 this way:

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:

inserir a descrição da imagem aqui

  • This is correct. What mistake are you making? Can you put it in your question as well?

  • This correct, plus if the user tries to open the page directly will generate an error

  • Yes, but what mistake?

  • modify its Action for ListaSelecionado(int? id) or ListaSelecionado(int id = 0) and make appropriate treatments for null or zero

  • I edited the answer.

1 answer

2

But in this Controller, if you have a new Action and you need a parameter, if you try to open this link, you will get an error. Is there any way to avoid this mistake?

I don’t understand what you mean by that, but if you don’t send the parameter to Action, will make a mistake anyway.

If we’re talking about a method like this,:

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

A course like this is expected:

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

This considering that your method is GET. If it is POST, id shall be completed only if the shipment is made from a form containing a <input> called id, or the request has in the data body a field called id.


EDIT

Note that the error you are having clearly states that the Action is asking for an integer parameter called id on the route. Therefore, access:

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

It’ll be a mistake, because id is not filled in. Now, if you pass any number after ListaSelecionado/:

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

It will work, as much as Id does not exist.

  • i am explaining better in my question! thank you

  • Ok, I understood your answer, I also know that if I pass any number will not give error, the problem and if the user tries to enter directly on this page after it is recorded on your computer, in this case it would have some route to direct the user stating that it was not possible or direct it to the index?

  • Ah, good. So you don’t let him enter directly through the address, use [HttpPost] on top of Action, but then you would have to embed a form in the link.

  • Oh good, you now gave me a super idea, more like using this embedded link and directing the user to a specific page, or is it possible an error page

  • That would be another question.

  • Okay, I’ll ask a new question

Show 1 more comment

Browser other questions tagged

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