You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC开发遇@bind未被识别报错,求技术协助

Troubleshooting the @bind Error in ASP.NET (MVC vs Blazor Confusion)

Hey there! Let's break down what's going on here—it sounds like you might be mixing up syntax between two different ASP.NET frameworks, which is super easy to do when you're starting out.

First off: @bind is a Blazor-specific directive, not something that works in traditional ASP.NET MVC Razor views. If you're following a tutorial that mentions Steve Sanderson (who's a key figure in both MVC and Blazor), there's a chance you accidentally switched between framework concepts without realizing it.

Let's address your scenario based on which framework you're actually working with:

If you're building an ASP.NET MVC app

Your existing code is for displaying a list of ToDo items, but if you were trying to use @bind to add edit functionality, MVC uses a different approach for model binding. Here's how to adjust your code to support editable items that will bind back to your controller:

First, make sure your view is using a ViewModel that includes your ToDoItems collection. Then, use a for loop (instead of foreach) so MVC can track each item with an index for proper binding:

<h4>(@Model.ToDoItems.Count)</h4>
<form asp-action="UpdateToDoList" method="post">
    <ul>
        @for (int i = 0; i < Model.ToDoItems.Count; i++)
        {
            <li>
                <!-- Use asp-for to bind to the item's Text property -->
                <input asp-for="ToDoItems[i].Text" />
                <!-- Include a hidden field for the item's ID (if you have one) to track which item to update -->
                <input type="hidden" asp-for="ToDoItems[i].Id" />
            </li>
        }
    </ul>
    <button type="submit">Save Changes</button>
</form>

In your controller, you'll have an action method that accepts your ViewModel to process the updated items:

[HttpPost]
public IActionResult UpdateToDoList(ToDoViewModel viewModel)
{
    // Save the updated ToDoItems to your database or data store
    // Redirect back to the view or another action
    return RedirectToAction("Index");
}

If you're actually working with Blazor (not MVC)

If you meant to build a Blazor app, the @bind syntax is valid—but you need to make sure you're working in a Blazor component (.razor file) and that your project is set up as a Blazor Web App, Blazor Server, or Blazor WASM project.

Here's a corrected version of your code for Blazor, including the necessary @code block to define your collection:

<h4>(@ToDoItems.Count)</h4>
<ul>
    @foreach (var item in ToDoItems)
    {
        <li>
            <!-- @bind works here in Blazor to create two-way binding -->
            <input @bind="item.Text" />
        </li>
    }
</ul>

@code {
    // Define your ToDoItems collection and model in the @code block
    private List<ToDoItem> ToDoItems = new()
    {
        new ToDoItem { Text = "Learn Blazor binding" },
        new ToDoItem { Text = "Fix the @bind error" }
    };

    public class ToDoItem
    {
        public string Text { get; set; } = string.Empty;
    }
}

Quick Check

Double-check your project type:

  • ASP.NET MVC projects have .cshtml views in the Views folder
  • Blazor projects have .razor components in the Components folder (or Pages for Blazor Server/WASM)

That mix-up is the most likely cause of your @bind not being recognized. Let me know if you need clarification on either framework's approach!

内容的提问来源于stack exchange,提问作者Ali Alkhumairi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:33:57