如何使用 select2 ASP.NET MVC 传递 2 个值
how to pass 2 value with select2 ASP.NET MVC
我有 1 个使用 Select2 的下拉列表,
我希望结果有 2 个值,ID 和电子邮件
这是我的控制器
public ActionResult Input(string input, string email)
{
// Populate your ViewModel to store each of those things
var model = new CostVM()
{
error = input,
email = email,
};
ViewData["List"] = new SelectList(db.depts, "id", "jenis");
return View(model);
}
[HttpPost]
public ActionResult Input(CostVM model)
{
FormCollection a = new FormCollection(Request.Unvalidated().Form);
var mail = db.depts.ToList();
model.input = string.Join(",", model.selec);
int ttd = 0;
for (ttd = 0; ttd < model.selec.Count(); ttd++)
{
var oke = db.depts.Find(Convert.ToInt32(model.selec[ttd]));
string[] idList = oke.email.Split(new char[] { ',' });
model.email = string.Join(",", idList);
}
return RedirectToAction("Input", "Select", new { input = model.input, email = model.email });
}
我的问题是,如果我添加 2 个或更多选项,只有一个添加到数据库
有人可以修复我的代码吗?抱歉我的英语不好
为了在 Entity Framework 中进行任何更改,您需要使用上下文 class 的 SaveChanges
方法(派生自 DbContext
class).我在你的代码中没有看到这样的命令(我猜对象 db
是上下文 class)。所以,我建议先尝试一下。
我认为您应该将 db.SaveChanges();
或 db.SaveChangesAsync();
放在 HTTP POST
版本的 Input
方法中,就在 return.
之前
替换
model.email = string.Join(",", idList);
和
model.email += string.Join(",", idList);
我有 1 个使用 Select2 的下拉列表,
我希望结果有 2 个值,ID 和电子邮件
这是我的控制器
public ActionResult Input(string input, string email)
{
// Populate your ViewModel to store each of those things
var model = new CostVM()
{
error = input,
email = email,
};
ViewData["List"] = new SelectList(db.depts, "id", "jenis");
return View(model);
}
[HttpPost]
public ActionResult Input(CostVM model)
{
FormCollection a = new FormCollection(Request.Unvalidated().Form);
var mail = db.depts.ToList();
model.input = string.Join(",", model.selec);
int ttd = 0;
for (ttd = 0; ttd < model.selec.Count(); ttd++)
{
var oke = db.depts.Find(Convert.ToInt32(model.selec[ttd]));
string[] idList = oke.email.Split(new char[] { ',' });
model.email = string.Join(",", idList);
}
return RedirectToAction("Input", "Select", new { input = model.input, email = model.email });
}
我的问题是,如果我添加 2 个或更多选项,只有一个添加到数据库
有人可以修复我的代码吗?抱歉我的英语不好
为了在 Entity Framework 中进行任何更改,您需要使用上下文 class 的 SaveChanges
方法(派生自 DbContext
class).我在你的代码中没有看到这样的命令(我猜对象 db
是上下文 class)。所以,我建议先尝试一下。
我认为您应该将 db.SaveChanges();
或 db.SaveChangesAsync();
放在 HTTP POST
版本的 Input
方法中,就在 return.
替换 model.email = string.Join(",", idList); 和 model.email += string.Join(",", idList);