I want to add a button that triggers the action on the Controller inside the razor CSHTML for my ASP.NET project.
Below is the section of the code
<div class="col-12 col-md-12 col-lg-6"> </div> <button type="submit" class="btn btn-warning w-100" asp-action="update" asp-route-id="edit" asp-route-id2="@item.Value"> <i class="fas fa-plus"></i>Update Template</button>
And the error I am getting is cannot convert from method group to object.
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
If you want to trigger the action on the Controller with HttpGet,you can use <a></a> tag:
<a asp-action="update" asp-route-id="edit" asp-route-id2="@item.Value"><i class="fas fa-plus"></i>Update Template</a>
If you want to trigger the action on the Controller with HttpPost,you can add a form outside button:
<form method="post">
<button type="submit" class="btn btn-warning w-100"
asp-action="update" asp-route-id="edit" asp-route-id2="@item.Value">
<i class="fas fa-plus"></i>Update Template
</button>
</form>
And the error I am getting is cannot convert from method group to
object.
For the error,I think it may caused by your backend code,may be somewhere you want to get an object from a method,and you forget to add ().Here is a demo:
public class A {
public int Id { get; set; }
public string Name { get; set; }
}
public A returnObject()
{
return new A { Id=1,Name="a"};
}
public void Update()
{
A o=returnObject;//Here you need to use A o=returnObject();
}
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