Mountain Man
Mountain Man

Reputation: 119

How do you execute C# code with a button click in asp.net?

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>

                    }
                 }

Upvotes: 0

Views: 1902

Answers (2)

Travis Acton
Travis Acton

Reputation: 4430

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/

Upvotes: 0

Win
Win

Reputation: 62260

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.

Upvotes: 0

Related Questions