Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.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# 如何将Linq值传递给控制器并传递给视图?_C#_Asp.net_Asp.net Mvc_Linq - Fatal编程技术网

C# 如何将Linq值传递给控制器并传递给视图?

C# 如何将Linq值传递给控制器并传递给视图?,c#,asp.net,asp.net-mvc,linq,C#,Asp.net,Asp.net Mvc,Linq,我无法将数据从模型显示到控制器,并且无法显示到视图中 模型客户 using System.Collections.Generic; namespace Train1.Models { public class Customer { public string CustomerId { get; set; } public string CustomerName { get; set; } public List<Custome

我无法将数据从模型显示到控制器,并且无法显示到视图中

模型客户

using System.Collections.Generic;
namespace Train1.Models
{
    public class Customer
    {
        public string CustomerId { get; set; }
        public string CustomerName { get; set; }
        public List<Customer> ListCustomer { get; set; }

        public void CustomerData()
        {
            ListCustomer.Add(new Customer { CustomerId = "C01", CustomerName = "Mike" });
            ListCustomer.Add(new Customer { CustomerId = "C02", CustomerName = "August" });
            ListCustomer.Add(new Customer { CustomerId = "C03", CustomerName = "Jhon" });
            ListCustomer.Add(new Customer { CustomerId = "C04", CustomerName = "Andie" });
        }
    }
}
控制器

using System.Web.Mvc;
using train1.Models;

namespace train1.Controllers
{
    public class CustomerController : Controller
    {
        // GET: Customer
        public ActionResult Index()
        {
            CustomerOrders cust = new CustomerOrders();
            return View();
        }
    }
}

如果希望视图从控制器接收模型,则需要传递该模型:

public ActionResult Index()
{
    return View(CustomerOrders.GetCustomer());
}
还要注意,
GetCustomer()
不会返回数据,请将其更改为:

public class CustomerOrders
{
    public static IEnumerable<Customer> GetCustomer() 
    {
        Customer cu = new Customer();
        cu.CustomerData();
        // from r in cu.ListCustomer select r
        return cu.ListCustomer; //no need to use LinQ here
    }
}
公共类客户订单
{
公共静态IEnumerable GetCustomer()
{
客户cu=新客户();
cu.CustomerData();
//从cu.ListCustomer中的r选择r
return cu.ListCustomer;//此处不需要使用LinQ
}
}

您没有将模型传递给视图。这与linq有什么关系?对不起,我更新了我的问题您的
客户订单
类与您的问题有什么关系?不,您在哪里调用
GetCustomer()
方法,但它将毫无意义,因为它
void
-它不会返回任何东西@StephenMuecke我假设OP仍在学习基础知识,
CustomerOrders
将代表一项服务,因此我给出了答案。我知道了,但我想从我的linq查询中得到,我已经更新了我的问题,
public class CustomerOrders
{
    public static IEnumerable<Customer> GetCustomer() 
    {
        Customer cu = new Customer();
        cu.CustomerData();
        // from r in cu.ListCustomer select r
        return cu.ListCustomer; //no need to use LinQ here
    }
}