You have a few options, normally with a postback you would submit the form with an <input type="submit" value="sendEmail" />
, the values in the form would be represented in a ViewModel like:
public class EmailFormViewModel()
{
public string value1 {get; set;}
public string reporter {get; set;}
//More properties on the form
}
Your endpoint would look like this:
[HttpPost]
public ActionResult SendEmail(EmailFormViewModel model)
{
//Send the email
}
If you still want to use a hyperlink to submit the form, which natively performs a GET request, you will can catch the click with javascript, and manually send the form via Ajax.
Something like:
$('#sendEmail').click(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
data: $('#formId').serialize(),
url: '/controllerName/sendemail'
}).done(function(response) {
//Do something on success response
});
});
Update:
You should also decorate your post action sendEmail with [ValidateAntiForgeryToken]
and add a @Html.AntiForgeryToken()
within the form. This will help protect against cross site forgery requests.
You can build your form, endpoint and model like this:
@using (Html.BeginForm("sendEmail"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<p>@Html.LabelFor(m => m.value1)</p>
<p>@Html.EditorFor(m => m.value1)</p>
<p>@Html.LabelFor(m => m.reporter)</p>
<p>@Html.EditorFor(m => m.reporter)</p>
<p>@Html.LabelFor(m => m.myDropDownListValue)</p>
<p>@Html.DropDownListFor(m => m.myDropDownListValue, Model.myList as SelectList)</p>
<p>@Html.LabelFor(m => m.myTextAreaValue)</p>
<p>@Html.TextAreaFor(m => m.myTextAreaValue, new { @cols = 150, @rows = 5})</p>
<input type="submit" value="Send Email"/>
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SendEmail(myModel model)
{
//Send the email
}
public class myModel
{
public IEnumerable<SelectListItem> myList { get; set; }
[DisplayName('Value 1')]
public string value1 { get; set; }
[DisplayName('Reporter')]
public string reporter { get; set; }
[DisplayName('Text Area')]
public string myTextAreaValue { get; set; }
[DisplayName('Dropdown')]
public string myDropDownListValue { get; set; }
}
As long as you are already on the same controller, it will postback to /controllername/sendemail
with the form data inside the post. You should also look up attributes on your models, you can enforce descriptions and validations for example. Check here for more details, its MVC 2 but still relevant.