问题描述
我正在尝试使用asp.net核心MVC创建一个简单的Todo MVC应用程序.我的计划是制作一个带有简单复选框的网页,并为用户提供使用表单更改状态的能力.表格显示正确,但是控制器中的模型绑定"不起作用.
I'm trying to make a simple todo MVC application with asp.net core MVC. My plan is to make a webpage with simple checkboxes and give the user the ability to change the state with a form.The form is displayed correctly but the "model binding" in the controller does not work.
我在做什么错了?
视图:
@model List<Todo>
<form asp-controller="Home" asp-action="ModifyTodo" method="post">
<ul class="mdc-list">
@for (var i = 0; i < Model.Count; i++)
{
<li class="mdc-list-item">
<input asp-for="@Model[i].Status" name="todos[@i].Status" >
<input asp-for="@Model[i].Id" name="todos[@i].Id" value="@Model[i].Id" type="hidden">
<input asp-for="@Model[i].Text" name="todos[@i].Text" value="@Model[i].Text" type="hidden">
<label> @Model[i].Text</label>
</li>
}
</ul>
<input type="submit" value="submit">
</form>
我的控制器:
public class HomeController : Controller
{
public IActionResult Index()
{
return GiveTodoView();
}
[HttpPost]
public IActionResult ModifyTodo([FromForm] List<Todo> todos)
{
// Console.WriteLine(test);
var temp = "Count: "+todos.Count;
foreach (var todo in todos)
{
temp += todo.Status + " " + todo.Text + " <br />";
}
return Content(temp);
// return GiveTodoView();
}
private IActionResult GiveTodoView()
{
var manager = TodoManager.GetTotalManager();
return View("Index", manager.GetTodos().ToList());
}
}
Todo.cs
public class Todo
{
public int Id;
public bool Status;
public string Text;
}
如何在控制器的操作ModifyTodo
中从表单中获取所有待办事项?
How do I get all my todo's from the form back in my controller's action ModifyTodo
?
提前谢谢!
修改
Razor生成的html.我已经删除了不必要的标记
The html generated by Razor. I've stripped the unnecessary markup
<form method="post" action="/Home/ModifyTodo">
<ul class="mdc-list">
<li class="mdc-list-item">
<input name="todos[0].Status" type="checkbox" checked="checked" data-val="true" data-val-required="The Boolean field is required." id="z0__Status" value="true">
<input name="todos[0].Id" value="0" type="hidden" data-val="true" data-val-required="The Int32 field is required." id="z0__Id">
<input name="todos[0].Text" value="First test" type="hidden" id="z0__Text">
<label>First test</label></li>
<li class="mdc-list-item">
<input name="todos[1].Status" type="checkbox" data-val="true" data-val-required="The Boolean field is required." id="z1__Status" value="true">
<input name="todos[1].Id" value="0" type="hidden" data-val="true" data-val-required="The Int32 field is required." id="z1__Id">
<input name="todos[1].Text" value="second test" type="hidden" id="z1__Text">
<label>second test</label></li>
</ul>
</div>
<input name="__RequestVerificationToken" type="hidden" value="CfDJ8GAD8c4qIkNEktrZwHPVewBTkkBNZ7RlAVcu7N2_sgbMy2O3FSRi_x5Nw1WNlXWqBhz_wNdeBYCj9oqnopS4fZxDgvjC5FGeCQ2jS3tS7IdXgwyAxPe6BnDhmOJDcK9fmqBdV8BcTC8qxrZEouj4RMU" />
<input name="[0].Status" type="hidden" value="false">
<input name="[1].Status" type="hidden" value="false">
</form>
推荐答案
由于todos
是您要绑定到的根对象的名称,因此在输入中不需要它的名称
Since todos
is the name of the root object you want to bind to you don't need its name on the inputs
<input asp-for="@Model[i].Status" name="[@i].Status" >
您还需要将其从公共领域更改为公共财产
You also need to change these from public fields to public properties
public class Todo
{
public int Id { get; set; }
public bool Status { get; set; }
public string Text { get; set; }
}
这篇关于使用ASP.NET Core进行模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!