简体   繁体   中英

ASP.Net Core Web App - cshtml file can't invoke Controller method

Trying to invoke a method on my controller from my index.cshtml file.

Html.BeginForm("Index", "Default", FormMethod.Get);

Where index is my method name, default is the controller and get is self explanatory. When I right click on "Index" and go to implementation it takes me to the method in my controller. However, when I debug the code it won't step into the Controller method and goes to the next line of code despite breakpoints obviously being in place and debug tool options set up correctly.

Also tried

<form method="get" action='@Url.Action("Index", "Default")'></form>

Similarly can't step into the controller.

How can I correctly invoke my controller method?

Form

An HTML <form> needs a submit button (and usually some controls) before it will call the controller action. It looks like the form in your example is empty.

You haven't shown the controller, but let's assume you want to pass a string to the controller action, maybe to search or filter:

public ActionResult Index(string searchTerm)
{
    // do something with parameters then return view
    return View();
}

Instead of this in your view:

Html.BeginForm("Index", "Default", FormMethod.Get);    // empty form

It should be something like this:

@Html.BeginForm("Index", "Default", FormMethod.Get) 
{
    // add controls here, what parameters are you passing? 
    @Html.TextBox("SearchTerm")
    <input type="submit" value="Search" />
}

Tag Helpers

Since you're using ASP.Net-Core you can take advantage of tag helpers which allow you to write code in a more HTML like manner. I encourage you to read Tag Helpers in forms in ASP.NET Core . One way the above could be written using tag helpers is:

<form asp-action="Index" asp-controller="Default" method="get">
    <input type="text" name="SearchTerm" />
    <button>Search</button>
</form>

ActionLink

Perhaps you want to create a hyperlink to Default/Index ? In that case, use the @Html.ActionLink helper:

@Html.ActionLink("go to this link", "Index", "Default")

Which will create a regular anchor <a> :

<a href="/Default">go to this link</a>

Tag Helper version

<a asp-action="Index" asp-controller="Default" >Click this link</a>

When you load index.cshtml,it would get into index action by default.You need to return model in your index action.

Here is a simple demo like below:

1.Model:

public class Test
{
    public int ID { get; set; }
    public string Title { get; set; }
}

2.Index.cshtml:

@model IEnumerable<TestModel>
<h1>Index</h1>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>            
        </tr>
}
    </tbody>
</table>

3.Index action:

public class TestModelsController : Controller
{
    private readonly MyDbContext _context;

    public TestModelsController(MyDbContext context)
    {
        _context = context;
    }

    // GET: TestModels
    public async Task<IActionResult> Index()
    {
        var model = await _context.TestModel.ToListAsync();
        return View(model);
    }
}

Reference: Passing data to views

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM