-2

I am trying to send email to the selected user. I have a sample user list with a checkbox, What I Want is: When a single user is checked and click on send email button then the emails goes to the concerned person and if multiple user is selected or checkall checkbox is clicked then email goes to all the selected user. Email address will be selected from the data base against selected user ID.

Below is the code I have tried so far, but I am confused and not able to understand it, I am New to Asp.Net + MVC 4, Any help will be appreciated.

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");
        }

My View Code:

    @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>

}
msz
  • 311
  • 1
  • 2
  • 16
  • 1
    You cannot use a `foreach` to generate form controls for a collection. And the parameter in you post method needs to be `List` in any case. Refer [Pass List of Checkboxes into View and Pull out IEnumerable](https://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for how you code should look –  Sep 26 '18 at 09:35

1 Answers1

0

Try the following:

Controller

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");
        }

Model code

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();
        }
    }
}

View code

@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>
}
Ami
  • 65
  • 7