How to call void functions from view .Net Core

I have a button that’s supposed to run a SQL query.

  <button class="btn btn-info mt-3"asp-controller="MessageCenter" asp-action="Markmessage">Mark</button>

there is no forms or any sort of thing that I can use a post method for. here is the function that I’m trying to call(this is in my controller by the way).

public void Markmessage()

        Messagelookupinfo messagelookup = new Messagelookupinfo();
        SqlConnection sql = new SqlConnection();
        SqlConnection sqlcon = new SqlConnection("data source");
        SqlCommand sqlcom = new SqlCommand("update messages set messagestatus='MARKED' where messageid=" + messagelookup.Messageid, sqlcon);
        RedirectToAction("Messagelookup", "Messageid=" + messagelookup.Messageid);

There is no feedback/confirmation for the user just straight up run the query and reload the page.
How do I achieve this?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I made a simple example, you can refer to it.

View (Messagelookup.cshtml):

    ViewData["Title"] = "Messagelookup";


<a class="btn btn-info mt-3" asp-controller="MessageCenter" asp-action="Markmessage" asp-route-messageid="2">Mark</a>


public class MessageCenterController : Controller
    public IActionResult Messagelookup(int? Messageid)
        ViewBag.Messageid = Messageid;
        return View();

    public IActionResult Markmessage(int? messageid)
        return RedirectToAction("Messagelookup", new { Messageid = messageid });


How to call void functions from view .Net Core

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x