以下是 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/