locked
How to call a controller method without routing to a view? RRS feed

  • Question

  • User651658926 posted

    Is it possible to have a method in your controller that you can call to do something operational, like say, delete records from the database but stay on the same view without needing to create an extra view?

    For example:
    I have the following method:

            public void DoSomeStuff(int id)
            {
               // do stuff here
            }

    I want to click a button on the current view to call the above method:
    However, if I did something like this, it will require a view:

    <a asp-controller="Customer" asp-action="DoSomeStuff" asp-route-id="@customer.CustomerId">Do Something</a>

    In a nut shell, how can I call a controller method without the need for a new view?

    Tuesday, October 20, 2020 7:18 PM

Answers

  • User1312693872 posted

    Hi,Amplitude

    Amplitude

    like say, delete records from the database but stay on the same view without needing to create an extra view?

    You can redirect to the current view when delete the records, you can check my following demo, it just need one index view:

    Controller:

    public IActionResult Index()
            {
                var model = _db.Students.ToList();
                return View(model);
            }
    
            [HttpPost]
            public async Task<IActionResult> Delete(int id)
            {
                var stu = await _db.Students.FindAsync(id);
                _db.Students.Remove(stu);
                await _db.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }

    View:

    @model IEnumerable<Student> 
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.name)
                </th>          
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.name)
                    </td>               
                    <td>
                        <form asp-action="Delete" asp-route-id="@item.Id">
                            <input type="submit" value="Delete" class="btn btn-danger" />
                        </form>
                    </td>
                </tr>
            }
        </tbody>
    </table>

    Result:

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 21, 2020 2:01 AM

All replies

  • User-474980206 posted

    You use JavaScript to make an Ajax call. The browser alone does not have to this feature.

    Wednesday, October 21, 2020 1:53 AM
  • User1312693872 posted

    Hi,Amplitude

    Amplitude

    like say, delete records from the database but stay on the same view without needing to create an extra view?

    You can redirect to the current view when delete the records, you can check my following demo, it just need one index view:

    Controller:

    public IActionResult Index()
            {
                var model = _db.Students.ToList();
                return View(model);
            }
    
            [HttpPost]
            public async Task<IActionResult> Delete(int id)
            {
                var stu = await _db.Students.FindAsync(id);
                _db.Students.Remove(stu);
                await _db.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }

    View:

    @model IEnumerable<Student> 
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.name)
                </th>          
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.name)
                    </td>               
                    <td>
                        <form asp-action="Delete" asp-route-id="@item.Id">
                            <input type="submit" value="Delete" class="btn btn-danger" />
                        </form>
                    </td>
                </tr>
            }
        </tbody>
    </table>

    Result:

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 21, 2020 2:01 AM
  • User651658926 posted

    Thanks so much Jerry!

    Wednesday, October 21, 2020 2:30 AM