我只是在问自己是否可以从 HttpGet Actionmethod 获取值并将它们传递给 HttpPost Actionmethod。
这是代码:
联系AdminController
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");
}
}
发送邮件.cshtml
@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; }
}
我的问题是我真的不知道如何将 EditorFors 和 TextAreaFor 中的值传递给 HttpPost SendMail 方法。我没有在 stackoverflow 上找到任何合适的东西,也没有从 asp.net 主页找到任何解决方案。我希望有人可以帮助我:)提前谢谢