1

I have button in my view.cshtml file:

<td>
    @if(@ViewBag.loading == false){
        @Html.ActionLink(
            "Run Test",
            "RunSingleTest",
            new { [email protected]_type, [email protected]},
            new { @class = "btn btn-info" })
    }
</td>

Which triggers method in HomeController.cs:

public IActionResult RunSingleTest(string testName, int id)
{
    _service.RunSingleTest(testName, id);
    var items = _service.GetDetailsById<List>(id);
    return View("TestDetails", items);
}

I want to disable this button when user clicks on that button. Should I do it with JQuery/Javascript? How do you pass that button id to script and where to put that id?

1
  • that is an anchor and anchors don't have disable property. You could either href="javascript:void(0) or return false via jQuery. Commented Aug 1, 2018 at 10:21

2 Answers 2

1

You can do it with plain HTML element. Also I don't see any benefit of disabling button on click since you want to let users know that this button should not perform more. Better put it disabled when it renders.

@if(ViewBag.loading == false){
  <a href="javascript:void(0)" class="btn btn-info btn-disabled" id="@item.Id" testName="@item.Test_type">Run Test</a>
}

or if you would like disable it literally then go with jQuery with giving an element a specific class -

<a href="/RunSingleTest" class="btn btn-info myBtn" ...> Run Test </a>

$('.myBtn').on('click',function(e){
  $(this).addClass('btn-disabled');
  e.PreventDefault() //preventing to navigate
});
Sign up to request clarification or add additional context in comments.

Comments

0

According to the question here you can not just set the disabled property on the actionlink. Instead here shows that you can do it with jQuery Javascript

@Html.ActionLink("Run Test", "RunSingleTest, 
       new { [email protected]_type, [email protected]}, 
       new { class = "btn btn-info linkdisabled" })

CSS

.linkdisabled{
   cursor:text;
}

JQuery

$function(){
    $(".linkdisabled").click(function(){
        return false;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.