问题描述
我是 ASP NET MVC
的新手
在使用 ASP NET MVC
检查当前用户是否启用应用程序后,我需要使用 MysqL 数据库和视图模型从数据库表中获取的值填充下拉列表。
这是tutorial
我下面的代码返回
Error: returns void,a return keyword must not be followed by an object expression
在这条线上
return items;
非常感谢任何帮助。
控制器
public ActionResult Recovery()
{
try
{
string cs = ConfigurationManager.ConnectionStrings["cnj"].ConnectionString;
using (var connection =
new MysqLConnection(cs))
{
string commandText = " SELECT cCountry FROM `dotable_user` " +
" WHERE cName = @Username; ";
using (var command =
new MysqLCommand(commandText,connection))
{
if (!String.IsNullOrEmpty(HttpContext.User.Identity.Name.ToString()))
{
command.Parameters.AddWithValue("@Username",HttpContext.User.Identity.Name.ToString());
}
connection.open();
string cCountry = (string)command.ExecuteScalar();
if (String.IsNullOrEmpty(cCountry))
{
TempData["Message"] = "No user.";
ViewBag.Message = String.Format("No user.");
}
List<SelectListItem> items = new List<SelectListItem>();
using (MysqLConnection con = new MysqLConnection(cs))
{
string query = " SELECT cCountry FROM `dotable_countries` " +
" WHERE cCountry = '" + cCountry.ToString() + "' ";
using (MysqLCommand cmd = new MysqLCommand(query))
{
cmd.Connection = con;
con.open();
using (MysqLDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
items.Add(new SelectListItem
{
Text = sdr["cCountry"].ToString(),Value = sdr["cCountry"].ToString()
});
}
}
connection.Close();
}
}
return items;
}
}
}
catch (Exception ex)
{
TempData["Message"] = "Login Failed.Error - " + ex.Message;
}
}
更新
我已尝试使用此代码。
Error CS0103 The name 'cCountry' does not exist in the current context
public ActionResult Recovery()
{
try
{
string cs = ConfigurationManager.ConnectionStrings["cnj"].ConnectionString;
using (var connection =
new MysqLConnection(cs))
{
string commandText = " SELECT cCountry FROM `dotable_user` " +
" WHERE cName = @Username; ";
using (var command =
new MysqLCommand(commandText,HttpContext.User.Identity.Name.ToString());
}
connection.open();
string cCountry = (string)command.ExecuteScalar();
if (String.IsNullOrEmpty(cCountry))
{
TempData["Message"] = "No user.";
ViewBag.Message = String.Format("No user.");
}
TempData["Dates"] = PopulateDates();
}
}
}
catch (Exception ex)
{
TempData["Message"] = "Login Failed.Error - " + ex.Message;
}
}
private static List<SelectListItem> PopulateDates()
{
List<SelectListItem> items = new List<SelectListItem>();
string cs = ConfigurationManager.ConnectionStrings["cnj"].ConnectionString;
using (MysqLConnection con = new MysqLConnection(cs))
{
string query = " SELECT cCountry FROM `dotable_countries` " +
" WHERE cCountry = '" + cCountry.ToString() + "'; ";
using (MysqLCommand cmd = new MysqLCommand(query))
{
cmd.Connection = con;
con.open();
using (MysqLDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
items.Add(new SelectListItem
{
Text = sdr["cCountry"].ToString(),Value = sdr["cCountry"].ToString()
});
}
}
cmd.Connection.Close();
}
}
return items;
}
解决方法
您没有将 cCountry 值传递给 populateDates。这就是您收到此错误的原因。您可以执行以下操作以填充下拉列表。然而,直接在控制器中编写业务逻辑并不是一个好主意。您应该将其移至模型或业务层。
int[] var = new int[]{getHighestCorrelation(y)};
Regression r=this.getRegression(y,var);
while (var.length < k) {
int inx = getBestCorrelation(r.getResidualArray());
var = Arrays.append(var,inx);
r = this.getRegression(y,var);
}
return var;
}
并在 Action 中调用此方法时,将国家/地区值传递给它,如下所示
TempData["Dates"] = PopulateDates(cCountry);