簡體   English   中英

聯系表單 ASP.NET C#

[英]Contact form ASP.NET C#

我正在嘗試在我的網頁中制作“聯系我們”表單,我嘗試了一些東西,我認為它應該可以工作,但我仍然遇到錯誤。

我試過這個:

家庭控制器.cs

   public class HomeController : Controller
    {
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Contact(ContactModel model, string email, string subject, string htmlMessage)
        {
            using (MailMessage mailMessage = new MailMessage())
            {

                mailMessage.From = new MailAddress("");
                mailMessage.Subject = subject;
                mailMessage.Body = htmlMessage;
                mailMessage.IsBodyHtml = true;
                mailMessage.To.Add(new MailAddress(email));
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp-mail.outlook.com";
                smtp.EnableSsl = true;
                System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                NetworkCred.UserName = "myemail@hotmail.com";
                NetworkCred.Password = "myEmailPassword";
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                await smtp.SendMailAsync(mailMessage);
            }
            return View(model);
        }

        public ActionResult SuccessMessage()
        {
            return View();
        }
    }

ContactModel.cs

public class ContactModel
{
    public string Email { get; set; }
    public string Name { get; set; }
    public string Message { get; set; }
}

聯系方式.csshtml

@model ContactForSpacess.Models.ContactModel


<h2>ContactMail</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Contact</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

我收到以下錯誤:應用程序中的服務器錯誤。 找不到資源。 描述:HTTP 404。

感謝我能得到的所有想法和幫助!

謝謝。

您只定義了接收表單提交的操作 - HttpPostAttribute 意味着無法通過 GET 請求訪問該操作。 假設您希望顯示的表單是您網站上的一個頁面,您需要定義一個 GET 操作:

public ActionResult Contact()
{
    var model = new ContactModel();

    return View(model);
}

如果你願意,你可以用[HttpGet]裝飾這個動作,但這不是真的必要。

如果需要,您還可以更新 Razor 助手以將表單方法顯式指定為 POST 並設置 action 屬性:

@using (Html.BeginForm("Contact", "Home", FormMethod.Post))
{
   @* ... your code *@
}

正如@asawyer 所指出的,HtmlHelper.BeginForm 擴展默認發出一個帶有 method="post" 的表單: https ://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.html.formextensions .beginform?view=aspnet-mvc-5.2#System_Web_Mvc_Html_FormExtensions_BeginForm_System_Web_Mvc_HtmlHelper _

應該是這樣的。

 [HttpGet]
    public async Task<IActionResult> Contact()
    {
        return View();
    }

    [HttpPost]
    [Route("postcontact")]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> ContactMakePost([FromBody] Contact model)
    {
        return View();
    }

你認為應該是這樣的

@using (Html.BeginForm("postcontact", "Home", FormMethod.Post)){ }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM