从[SQL]数据库中获取使用Asp.Net MVC4选中的复选框的记录

时间:2018-09-26 09:30:42

标签: asp.net-mvc asp.net-mvc-4

我正在尝试向选定的用户发送电子邮件。我有一个带有复选框的示例用户列表,我想要的是:当选中单个用户并单击“发送电子邮件”按钮时,电子邮件将发送给相关人员,并且如果选择了多个用户或全部选中单击复选框,然后电子邮件将发送给所有选定的用户。 电子邮件地址将根据所选的用户ID从数据库中选择。

  

下面是到目前为止我尝试过的代码,但是我很困惑并且无法   要了解它,我是Asp.Net + MVC 4的新手,将为您提供任何帮助   感谢。

My Model Code:

public void GetEmails(Employee data)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mvc"].ConnectionString);
            string qry = "select Email from users where Id IN(@id)";
            SqlCommand cmd = new SqlCommand(qry, con);
            con.Open();

            cmd.Parameters.AddWithValue("@id", data.IsChecked);
            cmd.ExecuteNonQuery();
            con.Close();
        }

My Controller Code:



public ActionResult Employee()  // Get Employee / Student Details
        {
            Employee emp = new Employee();

            List<Employee> students = emp.GetEE();

            return View(students);
        }

        [HttpPost]
        public ActionResult Employee(Employee data)  // Get Employee / Student Emails
        {
            Employee emp = new Employee();

            emp.GetEmails(data);

            return RedirectToAction("Employee");
        }

我的查看代码:

    @model List<mvcdemo.Models.Employee>
@{
    ViewBag.Title = "Employee";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script language="JavaScript">
    function toggle(source) {
        checkboxes = document.getElementsByName('data');
        for (var i = 0, n = checkboxes.length; i < n; i++) {
            checkboxes[i].checked = source.checked;
        }
    }
</script>
<h2>Employee</h2>

<h3>List of Employees</h3>
@Html.BeginForm()
{

<table class="table table-responsive table-responsive">
    <tr>

        <th>First Name</th>
        <th>Last Name</th>
        <th>User Name</th>
        <th>Email Name</th>
        <th>Actions</th>
        <th>Check All <input type="checkbox" name="checkall" onclick="toggle(this)" /></th>
    </tr>
    @foreach (var s in Model)
    {
    <tr>
        <td>@Html.CheckBoxFor(model => s.IsChecked, new { value=@s.Id, id="data" })</td>
        <td>@s.FName</td>
        <td>@s.LName</td>
        <td>@s.UName</td>
        <td>@s.Email</td>
        <td>@Html.ActionLink("Detail", "Details", "Home", new {id=@s.Id },null) / @Html.ActionLink("Edit", "Edit", "Home", new { id = @s.Id }, null) / @Html.ActionLink("Delete", "Delete", "Home", new { id = @s.Id }, null)</td>
       <td id="list"><input type="checkbox" name="data[]" id="data[]" value="@s.Id" /></td>
    </tr>
    }
    <tr>

        <td><button class="btn btn-primary" type="submit">Send Email</button></td>
    </tr>
</table>

}

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

控制器

public ActionResult Index()
        {
            //Generate random and bogus employees
            //Using NBuilder and Faker.Net nuget packages

            var employess = Builder<Employee>.CreateListOfSize(5)
            .All()
            .With(c => c.FName = Faker.Name.First())
            .With(c => c.LName = Faker.Name.Last())
            .With(c => c.UName = Faker.Internet.UserName())
                        .With(c => c.Email = Faker.Internet.Email())
            .With(c => c.IsChecked = Faker.RandomNumber.Next() % 2 == 0)
            .Build();
            return View(employess);
        }

        [HttpPost]
        public ActionResult Employee(IEnumerable<Employee> model)  // Get Employee / Student Emails
        {
            foreach (Employee emp in model)
            {
                if(!emp.IsChecked)
                {
                    continue;
                }
                emp.GetEmails();
            }

            return RedirectToAction("Index");
        }

型号代码

namespace mvcdemo.Models
{
    public class Employee
    {
        public int Id { get; set; }
        public string FName { get; set; }
        public string LName { get; set; }
        public string UName { get; set; }
        public string Email { get; set; }
        public bool IsChecked { get; set; }

        public string EmailContent { get; set; }

        //This is not really a place to have such a method
        internal void GetEmails()
        {
            EmailContent = string.Format("{0}-{1}", FName, LName);
            //SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mvc"].ConnectionString);
            //string qry = "select Email from users where Id IN(@id)";
            //SqlCommand cmd = new SqlCommand(qry, con);
            //con.Open();

            //cmd.Parameters.AddWithValue("@id", data.IsChecked);
            //cmd.ExecuteNonQuery();
            //con.Close();
        }
    }
}

查看代码

@model List<mvcdemo.Models.Employee>
@{
    ViewBag.Title = "Home Page";
}


<script language="JavaScript">
    function toggle(source) {
        checkboxes = document.querySelectorAll('#data');
        for (var i = 0, n = checkboxes.length; i < n; i++) {
            checkboxes[i].checked = source.checked;
        }
    }
</script>
<h2>Employee</h2>

<h3>List of Employees</h3>

@using (Html.BeginForm("Employee", "Home", FormMethod.Post))

{
    <table class="table table-responsive table-responsive">
        <tr>

            <th>First Name</th>
            <th>Last Name</th>
            <th>User Name</th>
            <th>Email Name</th>
            <th id="checkall">Check All <input type="checkbox" name="checkall" onclick="toggle(this)" /></th>
            <th>Actions</th>

        </tr>
        @{

            for (var i = 0; i < Model.Count; i++)
            {
                <tr>
                    @Html.HiddenFor(model => Model[i].Id)
                    <td>
                        @Html.EditorFor(o => Model[i].FName)
                    </td>
                    <td>@Html.EditorFor(o => Model[i].LName)</td>
                    <td>@Html.EditorFor(o => Model[i].UName)</td>
                    <td>@Html.EditorFor(o => Model[i].Email)</td>
                    <td>@Html.CheckBoxFor(model => Model[i].IsChecked, new { value = Model[i].Id, id = "data" })</td>
                    <td>@Html.ActionLink("Detail", "Details", "Home", new { id = Model[i].Id }, null) / @Html.ActionLink("Edit", "Edit", "Home", new { id = Model[i].Id }, null) / @Html.ActionLink("Delete", "Delete", "Home", new { id = Model[i].Id }, null)</td>

                </tr>
            }
        }

        <tr>

            <td><button class="btn btn-primary" type="submit">Send Email</button></td>
        </tr>
    </table>
}