0

I need help executing C# code with a button click. I`m sure there are multiple things wrong with this, however; any help would be much appreciated. Thank you for taking the time to view this post, and have a wonderful day!

            @{
                CommentController controller = new CommentController();

                UpVotes Upvote = new UpVotes(Misc.GetUser(), c.Postid);

            }


            @{
                if (controller.CheckVote(Upvote))
                {
                    <button type="button" onclick="@{

                        c.Upvotes += 1;

                        controller.PersonUpVote(Upvote);
                        }
                            }">
                        Like
                    </button>
                        }

                        else
                        {


                        <button type="button" onclick="@{

                            controller.PersonDownVote(Upvote);
                            c.Upvotes -= 1;
                        }

                                }">
                            Unlike
                        </button>

                    }
                 }
Mountain Man
  • 109
  • 1
  • 10
  • You can execute C# with AJAX call onclick. You can see an example [here](https://stackoverflow.com/questions/16186083/making-a-simple-ajax-call-to-controller-in-asp-net-mvc) – lloyd Jul 05 '17 at 22:29

2 Answers2

1

Scratch everything you've done so far as your heading down the wrong path.

On your view you need to attach to the button click via JavaScript then issue an Ajax call back to your controller.

See here http://www.makeitspendit.com/calling-asp-mvc-controllers-from-jquery-ajax/

Travis Acton
  • 4,292
  • 2
  • 18
  • 30
0

View should never instantiate a controller.

You lack the basics understanding of how ASP.NET MVC work or even basic HTML.

Please watch a free video training ASP.NET MVC 5 Fundamentals By Scott Allen at PluralSight.

Win
  • 61,100
  • 13
  • 102
  • 181