views:

78

answers:

1

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?

+2  A: 

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
Thanks...actually I realized that my the ActionLink doesn't work in either scenario. It works only when I have <input type="submit" value="Save" />.
Prabhu
@Prabhu, well, yes, that makes sense. That is what `[AcceptVerbs(HttpVerbs.Post)]` is *supposed* to do. Remove it and it would work.
Kirk Woll
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 <input type="submit..?
Prabhu
I found this: http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework
Prabhu
@Prabhu, the preferred way of handling this is by using two entirely separate <form> 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
@Prabhu, yes, that link looks like it will steer you in the right direction.
Kirk Woll
@Kirk, thanks !
Prabhu