0

我只是在问自己是否可以从 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 主页找到任何解决方案。我希望有人可以帮助我:)提前谢谢

4

1 回答 1

1

您不是在创建要提交给控制器的表单,是吗?您可以BeginForm FormExtension像这样在 Razor 中执行此操作

@using (Html.BeginForm("SendMail", "ContactAdmin", FormMethod.Post))
{
   //Your form here with submit button
}
  • 此方法将开始<form>标记写入响应。当用户提交表单时,请求将由一个 action 方法处理。
  • 您可以在一个using块中使用此方法。在这种情况下,该方法</form>在 using 块的末尾呈现结束标记。
  • 此方法有许多重载,但这允许定义FormMethodHTTP处理表单的方法,要么GET要么POST

所以,正如你想要POST的控制器, SendMail.cshtml将成为

@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>
}
于 2013-06-19T16:21:02.040 回答