Question

I'm guessing is very simple, but I'm learning MVC 2 right now and I'm stuck. I've got strongly typed view with some fields and buttons which should change something in database by click on them. So it is code

<% using (Html.BeginForm("UpVote", "Home",FormMethod.Post,new { linkId = link.LinkID }))
   {%>
        <input type="submit" value="UP" />
<% } %>

And my controller

[HttpPost]
public void UpVote(int linkId)
{
    var updateLink = geekDB.Link.Single(a => a.LinkID == linkId);
    updateLink.UpVotes++;
    geekDB.SaveChanges();

    RedirectToAction("Index");
}

And it doesn't work. When i press button, page is reloaded but nothing happens. I checked it with breakpoint in UpVote method but it never stop there and I have no idea why.

Was it helpful?

Solution

all your Actions should be ActionResult, you did void o_O

you could also put ViewResult,ContentResult RedirectRe.. but they are all ActionResult

OTHER TIPS

I would first check your routes.

Also your current use of BeginForm isn't proper as it would produce <form action="/Home/UpVote" linkid="yourlinkid" method="post"> when I suspect you want it to be something like <form action="/Home/UpVote?linkid=yourlinkid"...> - swapping the last two parameters will produce that output like so:

<% using (Html.BeginForm("UpVote", "Home",new { linkId = link.LinkID }, FormMethod.Post)) {%>
        <input type="submit" value="UP" />
<% } %>

The rest of your stuff looks just fine without knowing more about your project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top