It's not entirely clear what you want to do. Currently you are rendering multiple buttons in side a single form, but the form has no controls so nothing will post back (and since you have not specified the buttons type
attribute, it may not trigger a submit anyway depending on the browser). One way to solve this is to have a form (specifying a route parameter) for each item that posts back the email address and then redirects back to the index page.
@foreach (var item in Model)
{
<span>@item.EmailAddress</span>
@using (Html.BeginForm("ProcessEmail", new { emailAddress = item.EmailAddress }))
{
<button type="submit">Generate code</button>
}
}
and the POST method would be
[HttpPost]
public ActionResult ProcessEmail(string emailAddress)
{
// do something with the email
return RedirectToAction("Index"); // redisplay the page
}
Alternatively you could use a hidden input instead of a route parameter
@foreach (var item in Model)
{
<span>@item.EmailAddress</span>
@using (Html.BeginForm("ProcessEmail"))
{
@Html.HiddenFor(m => item.EmailAddress , new { id = "" }) // remove the id attribute to prevent invalid html
<button type="submit">Generate code</button>
}
}
However, to get far better performance and avoid having to regenerate the view each time, you can use ajax to post the value
@foreach (var item in Model)
{
<span>@item.EmailAddress</span>
<button type="button" class="process-email" data-email="@item.EmailAddress">Generate code</button>
}
var url = '@Url.Action("ProcessEmail")';
$('.process-email').click(function() {
$.post(url, { emailAddress: $(this).data('email') }, function(response) {
if(response) {
// processing succeeded - display message?
} else {
// processing failed = display error?
}
})
})
and modify the method to
[HttpPost]
public JsonResult ProcessEmail(string emailAddress)
{
// do something with the email
return Json(true); // or return Json(null) if an error occured
}