skip to Main Content

I’m trying to populate a table with member information such as name, email etc. I also have columns for entering a "warning" for members, and a textbox where more information about this warning is entered. You’re then supposed to press a button that posts this information, where it is then bound to a model and stored in a database.

By going the normal way and looping over the list and generating inputs for this based on the model, I run into issues (which I assume is due to the fact that there are multiple members in this table who all have the same inputs. This is the relevant code that I used in the MemberView.cshtml page:

@foreach (var user in Model.memberList)
        {
            <form asp-for="WebWarning" method="post">
                    <td>Regular member</td>
                    <td>
                        <select asp-for="WebWarning.WarningType" asp-items="Model.WarningTypes")"></select>
                    </td>
                    <td>
                        <input asp-for="WebWarning.Message"/>
                    </td>
                    <td>
                        <input asp-for="WebWarning.Person" type="hidden" value="@user" />
                        <button type="submit" class="btn btn-warning">Add Warning</button>

                    </td>
                    </form>
        }

Would anyone know how to get this to work? I’ve been trying (and failing) for hours now.

2

Answers


  1. I’m not sure if this is what you need, but you are missing the closing curly brace after your If Statement.

    Login or Signup to reply.
  2. From your description in the question, I think you wanna bind multiple values with the same name. In this case, We usually set the index manually. Because you don’t provide your model design, So here I write a simple demo, Hope it can help you solve this issue.

    Model

    public class User
        {
            public int Id { get; set; }
            public string Message { get; set; }
        }
    
    public class TestModel
        {
            public List<User> users { get; set; }
        }
    

    Controller

    public IActionResult Privacy()
            {
                TestModel test = new TestModel();
                test.users = new List<User>()
                {
                    new User()
                    {
                        Id = 1
                    },
                    new User()
                    {
                        Id = 2
                    },
                    new User()
                    {
                        Id = 3
                    },
                    new User()
                    {
                        Id = 4
                    },
                    new User()
                    {
                        Id = 5
                    }
                };
               
    
                return View(test);
            }
    

    View

    @model TestModel
    
    <form method="post">
        @for(var i=0; i < Model.users.Count; i++)
        {
            @*i configure the index manually*@
            <input name="users[@i].Id" value="@i" type="hidden"/>
            <input name="users[@i].message"/>
            <br />
        }
        <button type="submit">Submit</button>
    </form>
    

    Now I input the value into input tag and it will the value successfully.

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search