【问题标题】:Contact us form action not found未找到联系我们表单操作
【发布时间】:2020-01-05 16:03:03
【问题描述】:

不知道为什么这不起作用,但我怀疑这与路由有关......(使用 MVC5)

单击提交按钮时,我收到以下消息: 无法找到该资源。 说明:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请检查以下 URL 并确保其拼写正确。

请求的网址:/contact

使用模型如下:

public class EmailMessageModel
{
    /// <summary>Gets or sets from name.</summary>
    /// <value>From name.</value>
    [Required, Display(Name = "Name")]
    public string FromName { get; set; }
}

那么视图如下:

@model EmailMessageModel

@using (Html.BeginForm("index", "contact", FormMethod.Post, new { enctype = "multipart/form-data", @class = "contact-form" }))
{
    @Html.AntiForgeryToken()

    @Html.LabelFor(m => m.FromName, new { @class = "control-label" })
    @Html.TextBoxFor(m => m.FromName, new { @class = "form-control" })
    @Html.ValidationMessageFor(m => m.FromName)

    <input type="submit" value="Send Message" id="btnSubmitQuery" />
}

控制器如下:

(HttpPost Index 操作上的断点永远不会被命中,有什么想法吗?)

命名空间 ExternalSite.Controllers { 使用 ExternalSite.Models; 使用 System.Net.Mail; 使用 System.Web.Mvc;

[RoutePrefix("contact")]
public class ContactController : Controller
{
    [HttpGet]
    [Route]
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Index(EmailMessageModel model)
    {
        // !!!!!!!!!!BREAKPOINT HERE IS NEVER BEING HIT!!!!!!!!!!!
        if (ModelState.IsValid)
        {

        }

        return View(model);
    }
}

【问题讨论】:

    标签: asp.net-mvc model-view-controller asp.net-mvc-routing


    【解决方案1】:

    解决方法是在HttpPost Index方法中添加空白路由属性[Route],即

    [HttpPost]
    [Route]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Index(EmailMessageModel model)
    {
        // !!!!!!!!!!BREAKPOINT HERE IS NEVER BEING HIT!!!!!!!!!!!
        if (ModelState.IsValid)
        {
    
        }
    
        return View(model);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-05
      • 2022-01-24
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 2015-12-28
      • 1970-01-01
      相关资源
      最近更新 更多