asp.net-mvc-3 - ASP.NET MVC 3 RAZOR : How to use POST, 它给出错误 "Resource can not found"

标签 asp.net-mvc-3 razor get http-post

以下是 Controller 和 View 。当单击 [提交] 按钮时,应用程序会引发错误“找不到资源”。我知道使用 Get 和 Post 是 MVC 的非常基本的概念。在使用 Get 和 Post 时以及以下代码有什么问题时,是否可以通过一些实际场景让我清楚概念。

Controller :

namespace MVCModelBinding.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost, ActionName("Index")] 
        public ActionResult IndexPost()
        {
            if (Request.Form.Count > 0)
            {

                string id = Request.Form["ID"];

                string fname = Request.Form["FirstName"];

                string lname = Request.Form["LastName"];

                ViewBag.StatusMessage = "Employee data received successfully for ID " + id + "!";

            }

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

查看(index.chtml)

@using (Html.BeginForm("IndexPost", "HomeController",FormMethod.Post))
{
    <table>
        <tr>
            <td>
                Employee ID
            </td>
            <td>
                @Html.TextBox("ID")
            </td>
        </tr>
        <tr>
            <td>
                First name
            </td>
            <td>
                @Html.TextBox("FirstName")
            </td>
        </tr>
        <tr>
            <td>
                Last Name
            </td>
            <td>
                @Html.TextBox("LastName")
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="Submit" />
            </td>
        </tr>
    </table>
}

谢谢

保罗

最佳答案

用于表单的 Action 名称和 Controller 名称是错误的。应该是

@using (Html.BeginForm("Index", "Home", FormMethod.Post))

操作名称只是 "Index",因为这是您使用 ActionNameAttribute 指定的。 Controller 名称不应包含“Controller”后缀。

关于asp.net-mvc-3 - ASP.NET MVC 3 RAZOR : How to use POST, 它给出错误 "Resource can not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10563690/

相关文章:

apache - .htaccess重写仍然可以使用get变量的漂亮url

angularjs - Cordova/AngularJS $http.get 总是被缓存

asp.net-mvc-3 - 编译错误: "higher version than referenced assembly "

asp.net - 在哪里存储加密 key MVC 应用程序

c# - Ajax.BeginForm 导致重定向到部分 View 而不是就地 View

razor - 如何在 Razor 中设置只读属性?

c# - 如何将 MvcHtmlString 呈现为 HTML

asp.net-mvc-3 - 我可以将 PartialViewResult 与 PagedList 一起使用并将其显示在 PartialView 中吗

c# - OnPost 修改后 Razor Pages 模型值差异

javascript - windows.location.href 更改导致 POST,期望 GET