Model Binding with ASP.NET Core [duplicate]

喜你入骨 提交于 2020-05-27 13:18:51

问题


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.

What am I doing wrong?

The UI (image)

The view:

@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>

My Controller:

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;
}

How do I get all my todo's from the form back in my controller's action ModifyTodo?

Thanks in advance!

Edit

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>

回答1:


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; }
}



回答2:


You're prefixing your asp-for values with an @. This will cause Razor to actually get the value of that property, and place it there. That's not what you want. Instead, your tag helpers should be formatted as:

<input asp-for="[i].Status">


来源:https://stackoverflow.com/questions/46526433/model-binding-with-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!