帮助将 ActionLink 映射到控制器中的方法 (ASP.NET MVC2)
我正在进行我的第一个 MVC 项目,但仍然没有完全掌握它。我遇到了这个问题:
我的视图中有这个(Home/Index.aspx)
<% using (Html.BeginForm()) { %>
<fieldset>
<p>
<%: Html.TextBox("A")%>
<%: Html.TextBox("B") %>
<%: Html.ActionLink("Submit", "Create", "Home")%>
</p>
</fieldset>
<% } %>
我的控制器中有这个(Controllers/HomeController.cs)
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection formValues)
{
return View("Index");
}
我没有更改 global.asx 中的默认路由
当我点击提交时,我收到“找不到资源错误”。但是,如果我将 ActionLink 更改为
<input type="submit" value="Save" />
并将控制器中的方法更改为:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection formValues)
{
return View("Index");
}
它工作正常。
我有点困惑,因为如果我在 ActionLink 中指定确切的操作方法名称和控制器(<%: Html.ActionLink("Submit", "Create", "Home")%> ),为什么我将此方法命名为 Create 还是 Index 有关系吗?
I'm on my first MVC project and still haven't got a complete hang of it. I ran into this issue:
I have this in my View (Home/Index.aspx)
<% using (Html.BeginForm()) { %>
<fieldset>
<p>
<%: Html.TextBox("A")%>
<%: Html.TextBox("B") %>
<%: Html.ActionLink("Submit", "Create", "Home")%>
</p>
</fieldset>
<% } %>
I have this in my Controller (Controllers/HomeController.cs)
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection formValues)
{
return View("Index");
}
I haven't changed the default routes in global.asx
When I hit submit, I get the "The resource cannot be found error". However, if I change the ActionLink to
<input type="submit" value="Save" />
and the method in the controller to:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection formValues)
{
return View("Index");
}
it works fine.
I'm a little confused because if I'm specifying the exact action method name and the controller in the ActionLink (<%: Html.ActionLink("Submit", "Create", "Home")%> ), why would it matter whether I name this method Create or Index?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有
[AcceptVerbs(HttpVerbs.Post)]
将其限制为 HTTP POST 请求。由于操作链接是 GET,因此它没有使用您的方法。假设您有两个 Index 方法,其中之一没有该属性并接受 GET 请求。You have
[AcceptVerbs(HttpVerbs.Post)]
which restricts it to HTTP POST requests. Since an action link is a GET, it's not using your method. Presumably you have two Index methods, one of which doesn't have that attribute and accepts GET requests.