Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何单击客户';在actionResult中只显示他们的姓名?_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 如何单击客户';在actionResult中只显示他们的姓名?

C# 如何单击客户';在actionResult中只显示他们的姓名?,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,现在,我正在运行这个MVC项目。我有一个包含两个客户的表,它们被硬编码到表中,而不使用数据库。下面的两幅图显示了当您单击其中一位客户时应该发生的情况。在显示客户id时,它将使用操作结果详细信息调出以下URL“Customers/Details/1”。由于只有两个客户,如果将url更改为“customers/Details/3”,它将返回HttpNotFound错误。我已经包括了我的客户控制器、表的视图模型和路由配置文件。当我单击其中一个客户时,我会看到“未找到视图‘详细信息’或其主视图,或者没有

现在,我正在运行这个MVC项目。我有一个包含两个客户的表,它们被硬编码到表中,而不使用数据库。下面的两幅图显示了当您单击其中一位客户时应该发生的情况。在显示客户id时,它将使用操作结果详细信息调出以下URL“Customers/Details/1”。由于只有两个客户,如果将url更改为“customers/Details/3”,它将返回HttpNotFound错误。我已经包括了我的客户控制器、表的视图模型和路由配置文件。当我单击其中一个客户时,我会看到“未找到视图‘详细信息’或其主视图,或者没有视图引擎支持搜索的位置。搜索了以下位置:”


有一点问题是,您正在对内容进行硬编码,而不是将内容存储在某个地方,但为了举个例子,我也会这样做

首先,在控制器顶部,使用指令添加以下

using System.Linq;
将您的操作更改为:

public IActionResult Details(int id)
{
    // Remember that this is a different copy of the list
    // to the one in the Index action.
    var customers = new List<Customer>
    {
        // Notice I've added an Id property.
        new Customer { Id = 1, Name = "John Smith"},
        new Customer { Id = 2, Name = "Jane Doe"}
    };

    // Attempt to find a customer whose Id property is
    // equal to the id passed to the action.
    // SingleOrDefault() states that we expect to find only
    // one match if there is one, and if there isn't, it'll return `null`.
    var customer = customers
        .Where(x => x.Id == id)
        .SingleOrDefault();

    if (customer == null)
    {
        // Alternatively, you could return RedirectToAction("Index");
        return NotFound();
    }

    var viewModel = new CustomerViewModel
    {
        Customer = customer;
    };

    return View(viewModel);
} 
请注意,
Customer
是控制器的名称,
details
是操作的名称,它们都定义了一个
id
参数。这意味着该路由已经匹配默认的
“{controller}/{action}/{id}”
路由,因此您不需要
客户
路由

至于您对“详细信息”视图的评论,您的文件夹结构应如下所示:

Views
  \ Home
     \ Index.cshtml
  \ Customer
     \ Index.cshtml
     \ Details.cshtml

不过,在这一点上,我强烈建议您学习一些MVC教程,因为您目前似乎缺少一些基础知识。

有一点问题是,您是在硬编码,而不是将内容存储在某个地方,但为了举个例子,我也会这样做

首先,在控制器顶部,使用指令添加以下

using System.Linq;
将您的操作更改为:

public IActionResult Details(int id)
{
    // Remember that this is a different copy of the list
    // to the one in the Index action.
    var customers = new List<Customer>
    {
        // Notice I've added an Id property.
        new Customer { Id = 1, Name = "John Smith"},
        new Customer { Id = 2, Name = "Jane Doe"}
    };

    // Attempt to find a customer whose Id property is
    // equal to the id passed to the action.
    // SingleOrDefault() states that we expect to find only
    // one match if there is one, and if there isn't, it'll return `null`.
    var customer = customers
        .Where(x => x.Id == id)
        .SingleOrDefault();

    if (customer == null)
    {
        // Alternatively, you could return RedirectToAction("Index");
        return NotFound();
    }

    var viewModel = new CustomerViewModel
    {
        Customer = customer;
    };

    return View(viewModel);
} 
请注意,
Customer
是控制器的名称,
details
是操作的名称,它们都定义了一个
id
参数。这意味着该路由已经匹配默认的
“{controller}/{action}/{id}”
路由,因此您不需要
客户
路由

至于您对“详细信息”视图的评论,您的文件夹结构应如下所示:

Views
  \ Home
     \ Index.cshtml
  \ Customer
     \ Index.cshtml
     \ Details.cshtml

不过,在这一点上,我强烈建议您学习一些MVC教程,因为您目前似乎缺少一些基础知识。

您可以发布
详细信息
操作的真实代码吗?您显示的内容无法编译。这是真实的代码,我不确定该为actionResult放置什么。我相信是我的actionResult详细信息和路线配置有误。您可以发布
详细信息
操作的真实代码吗?你所展示的是不可编译的,这才是真正的代码,我不确定该为actionResult添加什么我相信这是因为我的actionResult详细信息和我的路线配置wrong@Soccerplayer97让我更新一下我的答案,告诉你答案应该在哪里-在评论中设置格式有点困难。我相信我已经找到了正确的解决方案,几天前,我已经完成了一些教程。我的路线根本不需要修改,我需要为我的客户和电影视图创建单独的视图模型。有什么方法可以让我与你分享我的源代码,或者我必须编辑这篇文章吗?@Soccerplayer97我真的很抱歉回复太晚-这是一个艰难的一周。对于类似的问题,您可以在codereview.stackexchange.com上发布。这样,您可以从多个来源获得反馈。@Soccerplayer97让我更新一下我的答案,告诉您答案应该在哪里-在评论中设置格式有点困难。我相信我已经找到了正确的解决方案,几天前我已经阅读了一些教程。我的路线根本不需要修改,我需要为我的客户和电影视图创建单独的视图模型。有什么方法可以让我与你分享我的源代码,或者我必须编辑这篇文章吗?@Soccerplayer97我真的很抱歉回复太晚-这是一个艰难的一周。对于类似的问题,您可以在codereview.stackexchange.com上发布。这样,您可以从多个来源获得反馈。
Views
  \ Home
     \ Index.cshtml
  \ Customer
     \ Index.cshtml
     \ Details.cshtml