【问题标题】:ASP.NET Core passing more than one parameters to viewASP.NET Core 传递多个参数查看
【发布时间】:2021-09-15 09:20:43
【问题描述】:

在我的项目中,我想从控制器向我的视图传递多个参数(id 和描述)。

这是我项目的结构:

ProductController:

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

Details.cshtml查看:

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

我知道我必须在 Startup.cs 中修改我的模式。如果我以这种方式修改它可以正常工作:

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

我的问题是:有没有更好的方法来做到这一点而不为每个参数添加“/”?

【问题讨论】:

  • 您可以将查询参数与 [FromQuery] 参数一起使用。使用这种方法,您无需更改路由定义。但更好的是想想你想做什么。为什么要使用参数来填充视图?
  • 您想将多个参数传递到控制器的操作方法还是视图中?
  • @Marco 我想从控制器的视图中传递更多参数。
  • @Dawid 我是 asp net core 的新手,这只是一个练习。
  • 您似乎在这里问了 2 个问题。一是关于如何在不添加自定义路由的情况下将多个参数传入DettaglioDescrizione,二是:如何将它们传递给视图。

标签: c# asp.net-core parameters routes


【解决方案1】:

模型绑定中有三个绑定源

  1. 表单值
  2. 路线价值
  3. 查询字符串

您现在正在做的是来自路由值,也许您可​​以使用查询字符串 /1?description=value 或者您可以执行 httppost 并从表单中获取值。

【讨论】:

  • 我是 asp net core 的新手,我不知道表单值和查询字符串,但我记住你的答案,我会正确修改我的代码,也许使用 httppost
  • @AntonioArgentieri 看看我上面的例子。它应该是您问题的完整解决方案。随意使用它并询问您是否还有其他问题。
【解决方案2】:

如果你想将多个参数从控制器传递到动作或从动作到控制器。你可以尝试创建一个模型。动作可以将数据与表单传递给控制器​​。动作返回一个模型来查看。这样你就不用了不需要通过路由或ViewData传递多个参数。这里有一个演示:

型号:

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

    }

行动:

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

详情查看:

@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>

查看:

@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>

结果:

【讨论】:

  • 非常感谢! :)
  • 如果我的回答对您有帮助,您能接受吗?谢谢。
【解决方案3】:

如果您不想更改路线定义。使用查询字符串参数。如果您也不想在操作方法中使用太多参数,请使用带有 [FromQuery] 注释的自定义类:

public class ProductController : Controller
{
    public IActionResult DettaglioDescrizione([FromRoute] id, [FromQuery] MyClassDto param)
    {
        ViewData["ProductId"] = id;
        ViewData["ProductDescription"] = descrizione;
        
        return View("Details");
    }
}

public class MyClassDto
{
    public int Description { get; set; }
}

路线定义

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

用法:

https://localhost/Product/DettaglioDescrizione/7?Description=Hello

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-05
    • 2021-02-09
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 2016-07-16
    • 2018-03-24
    • 2019-01-24
    相关资源
    最近更新 更多