Hi comunidadmexi...,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Model
public class EmailModel
{
public string Id { get; set; }
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View(new NorthwindEntities().Customers.Take(2).ToList());
}
public void ExportPDF(string id)
{
}
public ActionResult SendEmail(string id)
{
EmailModel model = new EmailModel();
model.Id = id;
if (id == "ALFKI")
{
model.To = "MariaAnders@gmail.com";
model.Subject = "Maria company name";
model.Body = "Alfreds Futterkiste";
}
if (id == "ANATR")
{
model.To = "AnaTrujillo @gmail.com";
model.Subject = "Ana company name";
model.Body = "Ana Trujillo Emparedados y helados";
}
return View(model);
}
[HttpPost]
public ActionResult Email(EmailModel model)
{
return View();
}
}
View
Index
@model IEnumerable<WebGrid_EF_MVC.Customer>
@{
Layout = null;
WebGrid webGrid = new WebGrid(source: Model, canPage: true, canSort: false);
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@webGrid.GetHtml(
htmlAttributes: new { @id = "WebGrid", @class = "Grid" },
columns: webGrid.Columns(
webGrid.Column("CustomerID", "Id"),
webGrid.Column("ContactName", "Name"),
webGrid.Column("City", "City"),
webGrid.Column("Country", "Country"),
webGrid.Column("", format: item => Html.ActionLink("PDF", "ExportPDF", new { id = item.CustomerID }, new { target = "_blank" })),
webGrid.Column("", format: item => Html.ActionLink("E-mail", "SendEmail", new { id = item.CustomerID }, new { target = "_blank" }))
))
</body>
</html>
SendEmail
@model WebGrid_EF_MVC.Models.EmailModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>SendEmail</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
@using (@Html.BeginForm("Email", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="row">
<div class="col-md-3">
<div class="form-group">
@Html.HiddenFor(model => model.Id)
@Html.LabelFor(model => model.To, "To", htmlAttributes: new { @Class = "Mytextarea2" })
@Html.TextBoxFor(m => m.To, new { @Class = "Mytextarea2", placeholder = "To" })
@Html.ValidationMessageFor(m => m.To, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
@Html.LabelFor(model => model.Subject, "Subject", htmlAttributes: new { @Class = "Mytextarea2" })
@Html.TextBoxFor(m => m.Subject, new { @Class = "Mytextarea2", placeholder = "To" })
@Html.ValidationMessageFor(m => m.Subject, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
@Html.LabelFor(model => model.Body, "Body", htmlAttributes: new { @Class = "Mytextarea2" })
@Html.TextBoxFor(m => m.Body, new { @Class = "Mytextarea2", placeholder = "To" })
@Html.ValidationMessageFor(m => m.Body, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<input type="submit" name="name" value="Send Email" />
}
</div>
</body>
</html>
Screenshot