ASP.NET MVC 2 - a simple increment value in a database represented in a view

I assume this is very simple, but now I'm learning MVC 2 and I'm stuck. I got a strongly typed view with some fields and buttons that should change something in the database by clicking on them. So this is the 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 does not work. When I click the button, the page reloads, but nothing happens. I tested it with a breakpoint in the UpVote method, but it never rests on its laurels, and I have no idea why.

+3
source share
2 answers

ActionResult, void o_O

ViewResult, ContentResult RedirectRe.. ActionResult

+3

.

BeginForm , <form action="/Home/UpVote" linkid="yourlinkid" method="post">, , , - <form action="/Home/UpVote?linkid=yourlinkid"...> - , :

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

, .

+1

Source: https://habr.com/ru/post/1767696/


All Articles