简体   繁体   English

ASP.Net Core Web App - cshtml 文件无法调用 Controller 方法

[英]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.试图从我的 index.cshtml 文件中调用我的 controller 上的方法。

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

Where index is my method name, default is the controller and get is self explanatory.其中 index 是我的方法名称,默认是 controller 并且 get 是不言自明的。 When I right click on "Index" and go to implementation it takes me to the method in my controller.当我右键单击“索引”和 go 来实现时,它会将我带到我的 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.但是,当我调试代码时,它不会进入 Controller 方法并转到下一行代码,尽管断点显然已经到位并且调试工具选项设置正确。

Also tried也试过

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

Similarly can't step into the controller.同样不能步入controller。

How can I correctly invoke my controller method?如何正确调用我的 controller 方法?

Form形式

An HTML <form> needs a submit button (and usually some controls) before it will call the controller action. HTML <form>在调用 controller 操作之前需要一个提交按钮(通常是一些控件)。 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:您尚未显示 controller,但假设您想将字符串传递给 controller 操作,可能是搜索或过滤:

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.由于您使用的是 ASP.Net-Core,因此您可以利用标签助手,它允许您以更像 HTML 的方式编写代码。 I encourage you to read Tag Helpers in forms in ASP.NET Core .我鼓励您阅读ASP.NET Core 中 forms 中的 Tag Helpers 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 ?也许您想创建指向Default/Index的超链接? In that case, use the @Html.ActionLink helper:在这种情况下,请使用@Html.ActionLink帮助器:

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

Which will create a regular anchor <a> :这将创建一个常规锚<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.当您加载 index.cshtml 时,默认会进入索引操作。您需要在索引操作中返回 model。

Here is a simple demo like below:这是一个简单的演示,如下所示:

1.Model: 1.Model:

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

2.Index.cshtml: 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: 3.索引动作:

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参考: 将数据传递给视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在ASP.NET应用程序中将.cshtml文件用作LoginURL吗? - Can I use a .cshtml file as the LoginURL in an ASP.NET app? Asp.net核心 - 调用方法()不会在中间件中调用 - Asp.net core - The invoke method() doesn't call in Middleware "ASP.NET Web App CORE:未调用控制器中的 HttpPost 编辑方法" - ASP.NET Web App CORE: the HttpPost Edit Method in Controller not being called ASP.Net Core 3.1 WEB API - Controller 方法不返回正确的 Z0ECD11C1D7A287401F814A8 - ASP.Net Core 3.1 WEB API - Controller method doesn't return correct JSON ASP.NET Core 中间件打破物理文件控制器方法 - ASP.NET Core Middleware Breaks Physical File Controller Method 如何重定向到 asp.net 核心中的另一个“.cshtml”文件? - How to redirect to another ".cshtml" file in asp.net core? 无法连接托管在 Azure Web 应用程序上的安全 Asp.Net Core Web 套接字(使用 TLS) - Can't connect secured Asp.Net Core Web Socket hosted on Azure Web App (using TLS) 在cshtml文件asp.net核心中缩小javascript - Minify javascript in cshtml file asp.net core ASP.NET Razor .cshtml文件是否在控制器中添加了“ if”检查? - ASP.NET Razor .cshtml file adding an 'if' check like in the controller? AuthenticationStateProvider 无法注入 ASP.NET Core Web App - AuthenticationStateProvider couldn't inject in ASP.NET Core Web App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM