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");
        sqlcon.Open();
        SqlCommand sqlcom = new SqlCommand("update messages set messagestatus='MARKED' where messageid=" + messagelookup.Messageid, sqlcon);
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        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?

Answers:

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";
}

<h1>Messagelookup</h1>

<div>
    <label>MessageId:</label>
    <h2>@ViewBag.MessageId</h2>
</div>
<a class="btn btn-info mt-3" asp-controller="MessageCenter" asp-action="Markmessage" asp-route-messageid="2">Mark</a>

Controller:
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 });
    }
}

Result:

How to call void functions from view .Net Core


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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x