0

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?

Prabhu
  • 12,995
  • 33
  • 127
  • 210

1 Answers1

2

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.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
  • Thanks...actually I realized that my the ActionLink doesn't work in either scenario. It works only when I have . – Prabhu Sep 24 '10 at 18:15
  • @Prabhu, well, yes, that makes sense. That is what `[AcceptVerbs(HttpVerbs.Post)]` is *supposed* to do. Remove it and it would work. – Kirk Woll Sep 24 '10 at 18:32
  • Ok, so I have a question...on the same view, I need to have two post buttons. Say for example, something like StackOverflow--on the same view, users can either CreateAnswer or CreateComment. How do you handle this with MVC since there is no way to specify the method name in an – Prabhu Sep 24 '10 at 18:36
  • I found this: http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework – Prabhu Sep 24 '10 at 18:52
  • @Prabhu, the preferred way of handling this is by using two entirely separate
    elements (BeginForm/EndForm), one for each button. (And if SO is a proper analog, that will work perfectly, as the comment text box and the answer text box are mutually exclusive.
    – Kirk Woll Sep 24 '10 at 18:53
  • @Prabhu, yes, that link looks like it will steer you in the right direction. – Kirk Woll Sep 24 '10 at 18:53