Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

asp.net - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...