我对C#有点新鲜,似乎我无法为我在控制器中创建的列表分配值。我试图从repo类中分配一个返回列表值的值。
我得到的信息是
没有任何论据符合所需的形式参数' client' ' Repo.SearchClient(ClientInfo)'
mY控制器:
public ActionResult SearchResult()
{
Repo repo = new Repo();
List<ClientInfo> searchResult = new List<ClientInfo>();
searchResult = repo.SearchClient(); // error here
JsonResult result = new JsonResult();
result.Data = searchResult;
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return result;
}
我的Repo类返回一个列表值
public List<ClientInfo> SearchClient(ClientInfo client)
{
var clientName = client.clientName;
List<ClientInfo> clientResult = new List<ClientInfo>();
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
try
{
SqlCommand command = new SqlCommand("SELECT * FROM Table_1 WHERE ClientName =@clientName", conn);
command.Parameters.AddWithValue("@clientName", clientName);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
ClientInfo data = new ClientInfo();
data.clientName = reader["ClientName"].ToString();
data.clientNumber = reader["ClientNumber"].ToString();
data.clientType = reader["ClientType"].ToString();
clientResult.Add(data);
}
}
catch
{
throw;
}
}
return clientResult;
}
我的模特
namespace ClientSearch.Models
{
public class ClientInfo
{
public string clientName { get; set; }
public string clientNumber { get; set; }
public string clientType { get; set; }
}
}
答案 0 :(得分:3)
您的搜索客户端方法需要ClientInfo作为参数。
public List<ClientInfo> SearchClient(ClientInfo client) // required parameter
调用方法时,控制器中的操作不提供此操作。
List<ClientInfo> searchResult = new List<ClientInfo>();
searchResult = repo.SearchClient(); // no parameter
编译时会出错。
要解决此问题,您需要执行以下操作:
var clientInfo = new ClientInfo()
{
ClientName = "test client"
}; // create a new ClientInfo object
var clientList = SearchClient(clientInfo); // call the search method and assign the results to a list