1

In my project I would like to pass more than one parameter (id and description) to my view from the controller.

This is the structure of my project:

ProductController:

public IActionResult DettaglioDescrizione(int id, string descrizione)
{
    ViewData["ProductId"] = id;
    ViewData["ProductDescription"] = descrizione;
    return View("Details");
}

Details.cshtml view:

<div class="text-center">
    <h1 class="display-4">Prodotti</h1>
    <p>Id prodotto: @ViewData["ProductId"]</p>
    <p>Descrizione prodotto: @ViewData["ProductDescription"]</p>
</div>

I know that I have to modify my pattern in Startup.cs. If I modify in this way it works properly:

app.UseEndpoints(endpoints =>
{
     endpoints.MapControllerRoute(
         name: "default",
         pattern: "{controller=Home}/{action=Index}/{id?}/{descrizione?}");
});

My question is: there is a better way to do this without add "/" for each parameter?

Dawid Wekwejt
  • 533
  • 1
  • 4
  • 19
  • You can use query parameters with [FromQuery] parameters. With this approach you don't need to change your routing definition. But much better is think about what You want to do. Why You want to use parameters to fill your view? – Dawid Wekwejt Sep 15 '21 at 09:31
  • Do you want to pass multiple parameters into the controllers action method or into the view? – Marco Sep 15 '21 at 09:34
  • @Marco I want to pass more parameters in my view from the controller. – Antonio Argentieri Sep 15 '21 at 09:35
  • 1
    @Dawid I'm new in asp net core, this is just an exercise. – Antonio Argentieri Sep 15 '21 at 09:35
  • 1
    It seems like you are asking 2 questions here. One is about how to get multiple parameters into `DettaglioDescrizione` without adding a custom route and the second one is: how to pass them to the view. – Marco Sep 15 '21 at 09:36
  • If You want change your route def. use query string parameters. If You want have many parameteres in your action method use a custom class with [FromQuery] adnotation: ActionResult Method([FromQuery] MyClass param){}. – Dawid Wekwejt Sep 15 '21 at 09:41
  • Does this answer your question? [How to pass multiple parameters to a get method in ASP.NET Core](https://stackoverflow.com/questions/36280947/how-to-pass-multiple-parameters-to-a-get-method-in-asp-net-core) – Jackdaw Sep 15 '21 at 11:30

2 Answers2

2

There are three binding sources in model binding

  1. Form Values
  2. Route Values
  3. Query string

what you are doing right now is from a route value, maybe you can use a query string /1?description=value or maybe you can do httppost and get the value from the form.

ChizT
  • 677
  • 3
  • 10
  • I'm new in asp net core, i don't know Form values and query strings but I keep in my mind your answer and I will modify my code properly, maybe using httppost – Antonio Argentieri Sep 15 '21 at 09:38
  • 1
    @AntonioArgentieri Take a look on my above example. It should be completed soulution for your question. Feel free with it and ask if You have more questions. – Dawid Wekwejt Sep 15 '21 at 10:02
1

If you want to pass multiple parameters from controller to action or from action to controller.You can try to create a Model.Action can pass data with a form to controller.Action returns a model to view.So that you don't need to pass more than one parameters with route or ViewData.Here is a demo:

Model:

public class Product
    {
        public int ProductId { get; set; }
        public string descrizione { get; set; }

    }

Action:

public IActionResult DettaglioDescrizione(Product product)
{
    return View("Details",product);
}

Details View:

@model Product
<div class="text-center">
    <h1 class="display-4">Prodotti</h1>
    <p>Id prodotto: @Model.ProductId</p>
    <p>Descrizione prodotto: @Model.ProductDescription</p>
</div>

View:

@model Product
<form method="post" asp-action="DettaglioDescrizione">
    <div class="form-group">
        <label asp-for="ProductId" class="control-label"></label>
        <input asp-for="ProductId" class="form-control" />
    </div>
    <div class="form-group">
        <label asp-for="ProductDescription" class="control-label"></label>
        <input asp-for="ProductDescription" class="form-control" />
    </div>
    <input type="submit" value="submit" />
</form>

result: enter image description here

Yiyi You
  • 16,875
  • 1
  • 10
  • 22