Receiving Request in View

Asked

Viewed 61 times

1

Hello! I am trying to receive a filter parameter in my view with the following code on Controller:

public ActionResult Cadastro(int? idParente)
    {
        var tab_Documentos_Parente = db.Tab_Documentos_Parente.Where(campo => campo.Id_Parente == idParente).Include(t => t.Tab_Documentos_Cadastrais_Par).Include(t => t.Tab_Parente);
        return View(tab_Documentos_Parente.ToList());
    }

But when I enter the url: http://localhost:53316/Documentation Simply does not run the filter. Making a Debugin the Controller I see that my parameter is always received as null.

It is not enough just to inform .../1 at the end of the URL? How then can I send the id I want to filter?

the model is as follows::

public partial class Tab_Documentos_Parente
{
    public int Id { get; set; }

    [Display(Name = "Nome do Parente")]
    public int Id_Parente { get; set; }

    [Display(Name = "Descrição do Documento")]
    public Nullable<int> Id_Documento { get; set; }

    [Display(Name = "Documento")]
    [DataType(DataType.Upload)]
    public byte[] Documento { get; set; }

    public virtual Tab_Documentos_Cadastrais_Par Tab_Documentos_Cadastrais_Par { get; set; }
    public virtual Tab_Parente Tab_Parente { get; set; }
}
  • Alam, you set the course for her to understand /1 is a parameter?

  • 1

    Try with the url http://localhost:53316/Documentos_Parente/Cadastro?idParente=1 if it works it’s because you didn’t set the route correctly.

  • You’re right @Leonardobonetti the url you suggested worked. But how it works with the Edit and Delete Views generated by Scaffold, I figured it would just follow the same template. Where I set up this route?

  • I’ll create a fucking answer, wait for me.

  • Kkkkk! Waiting for good...

1 answer

1


As I don’t know the whole hierarchy of your project I’ll give an example of my project.

I have a Homecontroller with the Image view(is an image result view):

public class HomeController : Controller
{
    public ActionResult Image(string id)
    {
        //Processa id
        return View(id);

    }
}

When I was going to call this view by passing the id I would have to order this: http://localhost:53316/Home/Image?id=1 however however, as I would already say that wise one called Google, nothing better than a friendly URL.

The friendly url would look like this: http://localhost:53316/Home/Image/1 But how to do this? You need to MAP a new route(Route).

To configure a new route, you must edit the file RouteConfig.cs in the briefcase App_start from the root of your project. As a default route you already have:

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

You will create a new route, do not delete the default route(For the love you have in your project), the new route would look like this:

routes.MapRoute(
            "Home",
            "Image/{produtoid}",
            new { controller = "Home", action = "Image" },
            new { produtoid = UrlParameter.Optional }
);

At this link you can see the Overload we use to map this route.

  • "Home" - name of your route.
  • "Image/{produtoid}" - the url pattern for your route.
  • new { controller = "Home", action = "Image" }, - you pass the controller/view that your route will be applied.
  • new { produtoid = UrlParameter.Optional } the parameters of the url

Now yes ! you can already use the question pattern: http://localhost:53316/Documentation , if you have set everything up correctly, with the correct names and etc everything will work. From what I understand of the little code you entered, the route would look like this:

routes.MapRoute(
        "Documentos_Parente",
        "Cadastro/{parenteid}",
        new { controller = "Documentos_Parente", action = "Cadastro" },
        new { parenteid= UrlParameter.Optional }
); 

I learned to do this in the post: Customizing Routes from José Carlos Macoratti’s website (which is a website VERY MUCH good for those who learn MVC, has plenty of content and easy to understand. I hope this resolves, I hope the return(comments) of you or anyone else with the same problem.

  • 1

    You really made a super response, cloned up the reference of msdn.microsoft.com and the macoratti website... There was no doubt. The route you suggested worked perfectly. Thank you very much.

  • @Still I thank you for the opportunity to help.

Browser other questions tagged

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