0

I'm trying to link a method defined in my razor pagemodel to an onclick event. Specifically, I'd like to do this without using extra javascript if possible.

I have the following razor code which fires the onclick event whenever the html is loaded for some reason. After the html is loaded, the onclick event doesn't work when I click something.

    <div class="list-group" id="module-list-tab" role="tablist">
        @for (int i = 0; i < Model.ModuleModel.Count; i++)
        {
            var module = Model.ModuleModel[i];
            <a class="list-group-item list-group-item-action" data-toggle="list" href="#list-tdc-tags" role="tab" onclick="@Model.UpdateTdcTags(module.ModuleID)">@Html.DisplayFor(indexItem => module.Name)</a>

        }
    </div>

And I have the following method defined in my pagemodel

    public async Task UpdateTdcTags(int moduleId)
    {
        TdcPointModel = await _context.TdcPoint.Where(x => x.ModuleId == moduleId).ToListAsync();
    }

Is it possible to link a PageModel method to an html onclick event? I'm aware that you can use handlers on forms for things like posting/getting, but i'd like to be able to tie other methods in PageModel to different types of html events.

  • 1
    FYI `onclick` _is_ Javascript. – ProgrammingLlama Apr 23 '20 at 01:08
  • If you don't want to use MVC or Javascript, what _do_ you want? – ProgrammingLlama Apr 23 '20 at 01:13
  • Does this answer your question? [HTML.ActionLink method](https://stackoverflow.com/questions/200476/html-actionlink-method) – David Federspiel Apr 23 '20 at 01:15
  • I was hoping to use method defined in Razor PageModel. Is it possible to use the HTML.ActionLink without a controller? FYI I know I could use a Handler for posting, but I was really curious if there was a simple way to tie a method in PageModel to different HTML events – Evan Miller Apr 23 '20 at 12:00
  • I think I found the answer to my question here https://learn.microsoft.com/en-us/aspnet/core/blazor/event-handling?view=aspnetcore-3.1 Just have to figure out how to combine blazor with my razor view – Evan Miller Apr 23 '20 at 20:04

0 Answers0