Receiving face in View

1

Hello! I'm trying to get a filter parameter in my view with the following code in 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 inform the url: link It simply does not run the filter. Making a Debug in Controller I see that my parameter is always being received as null .

Is not it just enough to tell ... / 1 to the end of the URL? How can I then send the id that I want to filter?

The template 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; }
}
    
asked by anonymous 17.08.2017 / 16:41

1 answer

1

As I do not know the whole hierarchy of your project I will give an example of my project.

I have a HomeController with the Image view (it's an image result view):

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

    }
}

When I was calling this view passing id I would have to request this: http://localhost:53316/Home/Image?id=1 however however, as I would say that sage 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 that? You need to MAP a new Route .

To set up a new route, you must edit the RouteConfig.cs file in the App_Start folder of 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 }
);

In this link you can see the overload we use for 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 } url parameters

Now, yes! you can already use the pattern of the question: link , if you have configured everything correctly, with the correct names and etc. everything will work. From what I understood from 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 site VERY MUCH for those who learn MVC, it has a lot of content and easy to understand.I hope this solves, I hope the feedback (comments) from you or any other with the same problem.     

17.08.2017 / 18:57