【问题标题】:Server Error in '/' Application in MVC 5 applicationMVC 5 应用程序中的“/”应用程序中的服务器错误
【发布时间】:2019-05-07 23:33:33
【问题描述】:

我目前正在通过观看视频教程学习 MVC 5。我创建了一个具有两种操作方法的简单客户控制器,即(AddCustomer 和 Submit)。在这里,我为 AddCustomer 创建了一个简单的视图,并为 Showing customer data 创建了一个强类型视图。当我启动应用程序时,它会显示客户数据输入屏幕,但是当我单击提交按钮时,我遇到了错误。 你能告诉我下面的代码有什么问题吗?

Server Error in '/' Application.

The resource cannot be found. 
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /Submit

这是我的客户控制器代码:-

public class CustomerController : Controller
    {
        // GET: Customer
        public ActionResult AddCustomer()
        {
            return View();
        }

        public ActionResult Submit(Customer objcust)
        {            
            objcust.CustomerID = Request.Form["txtcustid"];
            objcust.CustomerName = Request.Form["txtcustname"];
            return View("ShowCustData", objcust);
        }
    }

这是我的 ShowCustData 视图:-

@model DataAnnotationsEx.Models.Customer

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ShowCustData</title>
</head>
<body>
    <div>
        Customer ID: @Model.CustomerID <br />
        Customer Name: @Model.CustomerName
    </div>
</body>
</html>

这是我的 AddCustomer 视图:-

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>AddCustomer</title>
</head>
<body>
    <div>
        <form action="Submit" method="post">            
            Customer ID: <input id="Text1" type="text" name="txtcustid" /><br />
            Customer Name: <input id="Text1" type="text" name="txtcustname" /><br />
            <input id="Submit1" type="submit" value="submit" />
        </form>
    </div>
</body>
</html>

这是我的客户模型:-

public class Customer
    {
        [Required]
        [StringLength(7)]
        [RegularExpression("^[A-Z]{3,3}[0-9]{4,4}$")]
        public string CustomerID { get; set; }

        [Required]
        [StringLength(10)]
        public string CustomerName { get; set; }
    }

这是我的 Route.config:-

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Customer", action = "AddCustomer", id = UrlParameter.Optional }
            );
        }

【问题讨论】:

  • [HttpPost] 属性放在Submit 操作之上。乍一看,我看到表单动作有POST 方法,因此[HttpPost] 属性是强制性的。
  • @TetsuyaYamamoto 感谢您的快速回复。但它不起作用。仍然显示上述错误。
  • 由于这是 MVC,您应该使用带有控制器名称的 BeginForm 助手和带有 FormMethod.Post 参数的操作名称 - 避免手动创建 &lt;form&gt; 标签。

标签: asp.net-mvc asp.net-mvc-4 razor asp.net-mvc-5 asp.net-mvc-routing


【解决方案1】:

问题是您的Submit 控制器操作没有[HttpPost] 属性,您的表单方法定义为POST。您应该在相应的操作方法上添加提到的属性:

[HttpPost]
public ActionResult Submit(Customer objcust)
{            
    objcust.CustomerID = objcust.CustomerID;
    objcust.CustomerName = objcust.CustomerName;

    return View("ShowCustData", objcust);
}

注意,如果[HttpPost]属性默认不指定,它会自动设置为GET方法,因此表单提交请求永远不会到达action方法。

然后,您应该将&lt;form&gt; 标记替换为Html.BeginForm() 助手,使用强类型视图模型定义,如下所示:

@model Customer

@using (Html.BeginForm("Submit", "Customer", FormMethod.Post))
{
    @Html.LabelFor(model => model.CustomerID)
    @Html.TextBoxFor(model => model.CustomerID)

    @Html.LabelFor(model => model.CustomerName)
    @Html.TextBoxFor(model => model.CustomerName)

    <input id="Submit1" type="submit" value="submit" />
}

通过使用强类型视图模型类,文本框自动绑定到视图模型属性,并能够在表单提交期间检索值。

【讨论】:

  • 山本哲也感谢您的帮助。我已根据您的建议更改了代码,现在可以正常工作了。
  • 但我仍然想知道我的代码有什么问题。因为在教程中,导师在那里解释它的工作正常,但不是在我的最后。
  • 我已经解释了[HttpPost] 属性和action 属性路径成为主要问题,因为只有“提交”操作名称不足以提交请求,您应该添加控制器名称动作名称所属的位置。如果你用检查器检查更新的代码,你会清楚地看到差异。
猜你喜欢
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 2014-10-01
  • 1970-01-01
相关资源
最近更新 更多