[英]Getting error message: CS0029: Cannot implicitly convert (ASP.NET Core MVC & C#)
I'm getting that error message while trying to define my controller functions (I'm using ASP.NET Core 5.0).我在尝试定义 controller 函数时收到该错误消息(我使用的是 ASP.NET Core 5.0)。 I've been trying to solve it for a while but without success, I'm pretty much stuck.我一直在尝试解决它一段时间,但没有成功,我几乎被卡住了。
Take a look:看一看:
Controller functions showing error: Controller 函数显示错误:
public ActionResult Edit(int id)
{
using (Approver approver = dbContext.GetApproverbyId(id))
{
return View(approver);
}
}
public ActionResult Index()
{
List<approver> approverList = dbContext.GetAllapprovers().ToList();
return View(approverList);
}
public ActionResult Details(int id)
{
approver approver = dbContext.GetApproverbyId(id);
return View(approver);
}
My CONTEXT functions:我的上下文功能:
public IEnumerable<approver> GetAllapprovers()
{
var approverList = new List<approver>();
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("SP_GetAllApprovers", con)
{
CommandType = System.Data.CommandType.StoredProcedure
};
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
var approver = new approver
{
CODE = Convert.ToInt32(dr["CODE"].ToString()),
MAIL = dr["MAIL"].ToString()
};
approversList.Add(approver);
}
con.Close();
}
return approversList;
}
public approver GetApproverbyId(int? id)
{
var approver = new approver();
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("SP_GetapproverById", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CODE", id);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
approver.CODE = Convert.ToInt32(dr["CODE"].ToString());
approver.MAIL = dr["MAIL"].ToString();
}
con.Close();
}
return approver;
}
What am I doing wrong?我究竟做错了什么? Can help me to solve this?.可以帮我解决这个问题吗? I'm pretty lost我很迷茫
Lines showing error:显示错误的行:
dbContext.GetApproverbyId(id))
dbContext.GetAllapprovers().ToList();
dbContext.GetApproverbyId(id);
I think the problem made by your "connectionstring".SQL connection failed to pick your "appsettings.json's"file's connetionstring.我认为您的“connectionstring”.SQL 连接导致的问题未能选择您的“appsettings.json”文件的连接字符串。 for that you found error here因为你在这里发现了错误
dbContext.GetApproverbyId(id))
dbContext.GetAllapprovers().ToList();
dbContext.GetApproverbyId(id);
So you have to pick your connectionstring correctly.所以你必须正确选择你的连接字符串。
private IConfiguration Configuration;
public HomeController(IConfiguration _configuration)
{
Configuration = _configuration;
}
Now you have to some changes your context function现在你必须改变你的上下文 function
public IEnumerable<approver> GetAllapprovers()
{
var approveresLista = new List<approver>();
string connectionString= Configuration.GetConnectionString("YOUR CONNECTION STRING NAME");
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("SP_GetAllApprovers", con)
{
CommandType = System.Data.CommandType.StoredProcedure
};
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
var approver = new approver
{
CODE = Convert.ToInt32(dr["CODE"].ToString()),
MAIL = dr["MAIL"].ToString()
};
approversList.Add(approver);
}
con.Close();
}
return approversList;
}
Use your other context methods same as above.使用与上述相同的其他上下文方法。 I think it will work.if you have any issues with the above code.我认为它会起作用。如果您对上述代码有任何问题。 let me know.让我知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.