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