Reputation: 156
I am just asking myself wether it is possible to get values from an HttpGet Actionmethod and pass them to the HttpPost Actionmethod.
Here is the Code:
public class ContactAdminController : Controller
{
[HttpGet]
public ActionResult SendMail()
{
return View();
}
[HttpPost]
public ActionResult SendMail(ContactAdminViewModel contactAdmin)
{
if (ModelState.IsValid)
{
if (DQL.CheckUsernameAndEmail(contactAdmin.username, contactAdmin.email))
{
Mail.SendMail.SendForgotPassword(contactAdmin.username, contactAdmin.email, contactAdmin.message);
return RedirectToAction("LogIn", "Account");
}
}
else
{
ModelState.AddModelError("", "Your username is not associated with the email adress");
return RedirectToAction("LogIn", "Account");
}
return RedirectToAction("LogIn", "Account");
}
}
@model MvcApplication1.ViewModels.ContactAdminViewModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>SendMail</title>
</head>
<body>
<div>
<p>
@Html.LabelFor(m => m.username, "username")
@Html.EditorFor(m => m.username)
@Html.ValidationMessageFor(m => m.username)
</p>
<p>
@Html.LabelFor(m => m.email, "email")
@Html.EditorFor(m => m.email)
@Html.ValidationMessageFor(m => m.email)
</p>
<p>
@Html.LabelFor(m => m.message, "Your message")
<p>
@Html.TextAreaFor(m => m.message, new { cols = "35", rows = "10", @style = "resize:none" })
@Html.ValidationMessageFor(m => m.message)
</p>
</p>
<p>
@Html.ActionLink("Send", "SendMail", "ContactAdmin")
</p>
</div>
</body>
</html>
public class ContactAdminViewModel
{
[Required(ErrorMessage = "You need to fill in a username")]
public string username { get; set; }
[Required(ErrorMessage = "You need to fill in an email adress")]
public string email { get; set; }
[Required(ErrorMessage = "You need to fill a message for the admin")]
public string message { get; set; }
}
My problem is that I don't really know how to pass the values from the EditorFors and TextAreaFor to the HttpPost SendMail method. I didn't find anything appropriate on stackoverflow nor did I found any Solution from the asp.net Homepage. I hope someone could help me :) Thanks in advance
Upvotes: 0
Views: 1354
Reputation: 9458
You are not creating a form to submit to the controller, are you? You can do this in Razor by using BeginForm FormExtension
like this
@using (Html.BeginForm("SendMail", "ContactAdmin", FormMethod.Post))
{
//Your form here with submit button
}
<form>
tag to the response. When the user submits the form, the request will be processed by an action method.using
block. In that case, the method renders the closing </form>
tag at the end of the using block.FormMethod
:The HTTP
method for processing the form, either GET
or POST
.So, as you want to POST
to the controller, SendMail.cshtml
will become
@using (Html.BeginForm("SendMail", "ContactAdmin", FormMethod.Post))
{
<div>
<p>
@Html.LabelFor(m => m.username, "username")
@Html.EditorFor(m => m.username)
@Html.ValidationMessageFor(m => m.username)
</p>
<p>
@Html.LabelFor(m => m.email, "email")
@Html.EditorFor(m => m.email)
@Html.ValidationMessageFor(m => m.email)
</p>
<p>
@Html.LabelFor(m => m.message, "Your message")
<p>
@Html.TextAreaFor(m => m.message, new { cols = "35", rows = "10", @style = "resize:none" })
@Html.ValidationMessageFor(m => m.message)
</p>
</p>
<p>
<input type="submit" class="button" value="Send" />
</p>
</div>
}
Upvotes: 1