C# 在mvc中尝试将数据从actionmethod发送到静态方法

C# 在mvc中尝试将数据从actionmethod发送到静态方法,c#,asp.net-mvc,C#,Asp.net Mvc,我试图将ActionResult方法索引中的数据发送到MVC中的静态方法PopulatePlant,但在发送时出现错误: 该名称在当前上下文中不存在 基本上,在HttpPost方法中,我在Tempdata[“EmpId”]中接收到一个值,现在我想在“MasterPage”视图中发布这个值以及从PopulatePlant()返回的值 但首先,如何在静态方法PopulatePlant()中发送TempData[“EmpId”]中的值 [HttpPost] 公共行动结果索引(FormData个人) {

我试图将ActionResult方法索引中的数据发送到MVC中的静态方法PopulatePlant,但在发送时出现错误:

该名称在当前上下文中不存在

基本上,在HttpPost方法中,我在Tempdata[“EmpId”]中接收到一个值,现在我想在“MasterPage”视图中发布这个值以及从PopulatePlant()返回的值


但首先,如何在静态方法PopulatePlant()中发送TempData[“EmpId”]中的值

[HttpPost]
公共行动结果索引(FormData个人)
{
TempData[“EmpId”]=person.EmpId.ToString();
FormData Detail=新FormData();
Detail.PlantName=PopulatePlant();
返回视图(“母版页”);
}
私有静态列表PopulatePlant()
{
List PName=新列表();
String connectionString=ConfigurationManager.connectionString[“conndbprodnew”]。connectionString;
OracleConnection=新的OracleConnection(connectionString);
OracleCommand=new OracleCommand(“从Tdc_Product1中选择nvl(计数(1),0),其中Tdc_编号=:COLUMN1”,连接);
command.CommandType=CommandType.Text;
command.Parameters.AddWithValue(“:COLUMN1”,TempData[“EmpId]”);
返回PName;
}

任何想法都行。

您可以将值作为参数传递给您的方法,就像您在任何程序的方法和对象之间传递数据一样:

[HttpPost]
public ActionResult Index(FormData person)
{
 FormData Detail = new FormData();
 Detail.PlantName = PopulatePlant(person.EmpId.ToString()); //pass the relevant value to the method (using TempData is not really needed here)
 return View("MasterPage");
}

private static List<SelectListItem> PopulatePlant(string empId) //declare that the method will receive a string parameter
{
    List<SelectListItem> PName = new List<SelectListItem>();
    String connectionString = ConfigurationManager.ConnectionStrings["conndbprodnew"].ConnectionString;
    OracleConnection connection = new OracleConnection(connectionString);
    OracleCommand command = new OracleCommand("select nvl(count(1),0) from Tdc_Product1 where TDC_NO=:COLUMN1", connection);
    command.CommandType = CommandType.Text;
    command.Parameters.AddWithValue(":COLUMN1", empId); //use the method's parameter
    return PName;
}
[HttpPost]
公共行动结果索引(FormData个人)
{
FormData Detail=新FormData();
Detail.PlantName=PopulatePlant(person.EmpId.ToString());//将相关值传递给该方法(此处实际上不需要使用TempData)
返回视图(“母版页”);
}
private static List PopulatePlant(string empId)//声明该方法将接收字符串参数
{
List PName=新列表();
String connectionString=ConfigurationManager.connectionString[“conndbprodnew”]。connectionString;
OracleConnection=新的OracleConnection(connectionString);
OracleCommand=new OracleCommand(“从Tdc_Product1中选择nvl(计数(1),0),其中Tdc_编号=:COLUMN1”,连接);
command.CommandType=CommandType.Text;
command.Parameters.AddWithValue(“:COLUMN1”,empId);//使用方法的参数
返回PName;
}

当前上下文中不存在哪个名称
Plant
方法未返回任何值。您可能希望从更正它开始。为什么该方法是静态的?另外,您不能在发布的模型FormData中获取EmpID吗?如果是这样,请向PopulatePlant添加一个参数并将其传入!您应该执行
Detail.PlantName=.PopulatePlant()“如何在静态方法内部发送TempData[“EmpId”]中的值”…就像其他任何方法一样:使用参数
私有静态列表PopulatePlant(string EmpId)
声明该方法,并在调用时传入一个值:
Detail.PlantName=PopulatePlant(TempData[“EmpId”].ToString()最后,您可以执行
命令.Parameters.AddWithValue(“:COLUMN1”,empId)使用方法参数。将参数传递给方法通常是您在编程的第一天学到的东西……很难知道您的实际困难到底是什么?@ChetanRanpariya,如果我将值存储在索引方法中,如int-EmpId=person.EmpId;并像command.Parameters.AddWithValue(“:COLUMN1”,person.EmpId.ToString())一样使用它;然后它抛出错误,或者如上所示,如果我尝试使用Tempdata[“EmpID”],那么它也抛出错误
[HttpPost]
public ActionResult Index(FormData person)
{
 FormData Detail = new FormData();
 Detail.PlantName = PopulatePlant(person.EmpId.ToString()); //pass the relevant value to the method (using TempData is not really needed here)
 return View("MasterPage");
}

private static List<SelectListItem> PopulatePlant(string empId) //declare that the method will receive a string parameter
{
    List<SelectListItem> PName = new List<SelectListItem>();
    String connectionString = ConfigurationManager.ConnectionStrings["conndbprodnew"].ConnectionString;
    OracleConnection connection = new OracleConnection(connectionString);
    OracleCommand command = new OracleCommand("select nvl(count(1),0) from Tdc_Product1 where TDC_NO=:COLUMN1", connection);
    command.CommandType = CommandType.Text;
    command.Parameters.AddWithValue(":COLUMN1", empId); //use the method's parameter
    return PName;
}