简体   繁体   English

帮助将ActionLink映射到控制器中的方法(ASP.NET MVC2)

[英]Help with mapping ActionLinks to Methods in Controllers (ASP.NET MVC2)

I'm on my first MVC project and still haven't got a complete hang of it. 我正在进行我的第一个MVC项目,但还没有完全了解它。 I ran into this issue: 我遇到了这个问题:

I have this in my View (Home/Index.aspx) 我的视图中有这个(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) 我的控制器中有这个(Controllers / HomeController.cs)

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection formValues)
{
    return View("Index");
}

I haven't changed the default routes in global.asx 我没有更改global.asx中的默认路由

When I hit submit, I get the "The resource cannot be found error". 单击提交时,出现“找不到资源错误”。 However, if I change the ActionLink to 但是,如果我将ActionLink更改为

<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? 我有些困惑,因为如果我在ActionLink中指定确切的操作方法名称和控制器(<%:Html.ActionLink(“ Submit”,“ Create”,“ Home”)%>),为什么会这样将此方法命名为Create还是Index无关紧要?

You have [AcceptVerbs(HttpVerbs.Post)] which restricts it to HTTP POST requests. 您有[AcceptVerbs(HttpVerbs.Post)]其限制为HTTP POST请求。 Since an action link is a GET, it's not using your method. 由于操作链接是GET,因此未使用您的方法。 Presumably you have two Index methods, one of which doesn't have that attribute and accepts GET requests. 大概您有两个 Index方法,其中一个不具有该属性并且可以接受GET请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM