如何在表单提交时将复选框值传递给控制器然后 return 要查看的数据?
how to pass checkbox value to controller on form submit then return data to view?
在我看来,我在一些用户记录前面放置了一个复选框。当我选中一个复选框并单击提交按钮时,选中的复选框值(用户 ID ) 将被发送到控制器。目前我在我的模型中成功收到了电子邮件,但无法在视图中返回数据库结果。
客户视图:
@using (Html.BeginForm())
{
<table class="table table-responsive">
@foreach (var s in Model)
{
<td id="list"><input type="checkbox" name="ids" id="ids" value="@s.Id" /></td>
}
<tr>
<td><button class="btn btn-primary" id="sendmail" type="submit">Send Email To Customers</button></td>
</tr>
</table>
}
@if (TempData["Emails"] != null)
{
<span class="alert-info">@TempData["Emails"]</span>
}
控制器:
[HttpPost]
public ActionResult Customers(int[] ids)
{
Customers cu= new Customers();
cu.IDS = ids;
cu.GetEmails(ids);
TempData["ids"] = string.Join(",", ids.ToArray());
TempData["Emails"] = cu.GetEmails(ids).Email.ToArray(); // I want these emails in my view
return RedirectToAction("Customers");
}
型号:
public Customers GetEmails(int[] ids)
{
Customers ee = new Customers();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mvc"].ConnectionString);
string qry = "select Email from users where Id IN(" + string.Join(",", ids.ToArray()) + ")";
SqlCommand cmdd = new SqlCommand(qry, con);
con.Open();
SqlDataReader rdd = cmdd.ExecuteReader();
while (rdd.Read())
{
ee.Email = rdd["Email"].ToString(); // I am able to get all emails
}
rdd.Close();
cmdd.ExecuteNonQuery();
con.Close();
return ee;
}
好吧,让我们从一些虚拟数据和测试用例场景开始
public ActionResult Test()
{
return View();
}
[HttpPost]
public ActionResult Test(MyModel model)
{
TempData["test"] = GetData();
return View();
}
public List<string> GetData() //method for get data
{
List<string> mylist = new List<string>(new string[] { "element1", "element2", "element3" });
return mylist;
}
和剃须刀视图
var data = TempData["test"] as List<string>;
if (data != null)
{
foreach (var a in data)
{
WriteLiteral(@"<li>"); /**/
Write(a); /**/
WriteLiteral(@"</li>");
}
}
现在根据你的场景,我假设你的 GetEmail
只有 return 一组字符串,所以将你的 return 类型更改为 Customer object to list<string>
as public List<string> GetEmails()
和 return ee
作为字符串列表。
然后您需要在 razor 视图中执行所有操作,例如测试用例场景。
在我看来,我在一些用户记录前面放置了一个复选框。当我选中一个复选框并单击提交按钮时,选中的复选框值(用户 ID ) 将被发送到控制器。目前我在我的模型中成功收到了电子邮件,但无法在视图中返回数据库结果。
客户视图:
@using (Html.BeginForm())
{
<table class="table table-responsive">
@foreach (var s in Model)
{
<td id="list"><input type="checkbox" name="ids" id="ids" value="@s.Id" /></td>
}
<tr>
<td><button class="btn btn-primary" id="sendmail" type="submit">Send Email To Customers</button></td>
</tr>
</table>
}
@if (TempData["Emails"] != null)
{
<span class="alert-info">@TempData["Emails"]</span>
}
控制器:
[HttpPost]
public ActionResult Customers(int[] ids)
{
Customers cu= new Customers();
cu.IDS = ids;
cu.GetEmails(ids);
TempData["ids"] = string.Join(",", ids.ToArray());
TempData["Emails"] = cu.GetEmails(ids).Email.ToArray(); // I want these emails in my view
return RedirectToAction("Customers");
}
型号:
public Customers GetEmails(int[] ids)
{
Customers ee = new Customers();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mvc"].ConnectionString);
string qry = "select Email from users where Id IN(" + string.Join(",", ids.ToArray()) + ")";
SqlCommand cmdd = new SqlCommand(qry, con);
con.Open();
SqlDataReader rdd = cmdd.ExecuteReader();
while (rdd.Read())
{
ee.Email = rdd["Email"].ToString(); // I am able to get all emails
}
rdd.Close();
cmdd.ExecuteNonQuery();
con.Close();
return ee;
}
好吧,让我们从一些虚拟数据和测试用例场景开始
public ActionResult Test()
{
return View();
}
[HttpPost]
public ActionResult Test(MyModel model)
{
TempData["test"] = GetData();
return View();
}
public List<string> GetData() //method for get data
{
List<string> mylist = new List<string>(new string[] { "element1", "element2", "element3" });
return mylist;
}
和剃须刀视图
var data = TempData["test"] as List<string>;
if (data != null)
{
foreach (var a in data)
{
WriteLiteral(@"<li>"); /**/
Write(a); /**/
WriteLiteral(@"</li>");
}
}
现在根据你的场景,我假设你的 GetEmail
只有 return 一组字符串,所以将你的 return 类型更改为 Customer object to list<string>
as public List<string> GetEmails()
和 return ee
作为字符串列表。
然后您需要在 razor 视图中执行所有操作,例如测试用例场景。