views:

39

answers:

2

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.

+1  A: 

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.

BuildStarted
Yeah, it also could be a problem, but main issue was in not returning a thing. But thanks :)
karol
+3  A: 

all your Actions should be ActionResult, you did void o_O

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

Omu
It helped. I didn't know that I always need to return something. I wanted just to increment one thing.
karol
well, in your case you return RedirectToAction("Index")
Omu
Wow, I can't believe I didn't notice that.
BuildStarted