【问题标题】:Passing Parameters to Index List Controller in MVC Core将参数传递给 MVC Core 中的索引列表控制器
【发布时间】:2019-07-18 13:36:45
【问题描述】:

我对这段代码有疑问,它总是给我空结果:

        public async Task<IActionResult> Index(int? sipno)
    {
        var kolaycam_acsgoContext = _context.siparis_detay
            .Where(s => s.Siparis_no == sipno);
        return View(await kolaycam_acsgoContext.ToListAsync());
    }

但如果我不使用这样的参数,它会起作用。我做错了什么?

        public async Task<IActionResult> Index()
    {
        var kolaycam_acsgoContext = _context.siparis_detay
            .Where(s => s.Siparis_no == 901120);
        return View(await kolaycam_acsgoContext.ToListAsync());
    }

【问题讨论】:

  • 如何从视图中调用 Index 方法?
  • sipno 的值是多少?你调试了吗?
  • 我在我的第二个代码中使用具有相同值的“sipno”。顺便说一句,“localhost:44364/siparis_detay/index/901120”是我的网址。
  • 除非您将sipno 设置为路由值,否则您需要使用"localhost:44364/siparis_detay/index?sipno=901120" 来实现您所展示的工作。
  • 您可以使用int? ids.Siparis_no == id[FromRoute(Name="id")] int? sipno

标签: c# asp.net entity-framework asp.net-core-mvc


【解决方案1】:

"Merhaba Tuğhan hocam nasılsın ? (:"

我认为问题是变量类型 int? sipno 变量可能为空 另一个问题是 s => s.Siparis_no 属性类型

它是一个 int? 吗?如果比较的类型不重要的话

你调试了吗?如果你这样做了,sipno

的价值是什么

请查看此答案并报告您的结果

【讨论】:

  • "Merhaba Baran teşekkür ederim sen?:)" 我正在使用 "localhost:44364/siparis_detay/index/901120" 它必须给我相同的结果,但它没有。类型是 int,我不知道我们为什么使用 ?但我使用它是因为其他样本。在我的第二个代码中,一切正常。
  • "Teşekkürler (:" intint? 不同的类型。如果 sipno 类型是 int ? 那么 s.Siparis_no 必须是相同的类型。请告诉我调试结果和 sipno 变量值?
【解决方案2】:

我发现了这个愚蠢的错误,当我将 sipno 更改为 id 时,它起作用了。

public async Task<IActionResult> Index(int? id)
{
    var kolaycam_acsgoContext = _context.siparis_detay
        .Where(s => s.Siparis_no == id);
    return View(await kolaycam_acsgoContext.ToListAsync());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 2010-09-14
    • 1970-01-01
    • 2022-10-31
    • 1970-01-01
    • 2020-03-20
    相关资源
    最近更新 更多