1

first i create list of models in [HttpGet] action and return to view.

public ActionResult Setting()
{
        var model = db.Settings.ToList();
        return View(model);
}

them in view get list in view and show good. but after edit the value of setting i want to pass list of object from view to controller don't work this.

@model List<TajerWebsite.Models.Setting>
<!-- Main content -->
<section class="content">
    <div class="container-fluid">
        <div class="row">
            <!-- left column -->
            <div class="col-md-6">
                <!-- general form elements -->
                <div class="card card-primary">
                    <div class="card-header">
                        <h3 class="card-title">تنظیمات</h3>
                    </div>
                    <!-- /.card-header -->
                    <!-- form start -->
                    @using (Html.BeginForm("Setting","Admin", new { FormMethod.Post }))
                    {

                        @Html.AntiForgeryToken()
                        foreach (var item in Model)
                        {
                            <div class="card-body">
                                <div class="form-group">
                                    @Html.HiddenFor(model => item.Id)
                                    <label for="exampleInputPassword1">@item.Name</label>
                                    @Html.EditorFor(model => item.Value, new { htmlAttributes = new { @class = "form-control"} })
                                    @Html.ValidationMessageFor(model => item.Value, "", new { @class = "text-danger" })
                                </div>

                            </div>
                        }

                        <!-- /.card-body -->

                        <div class="card-footer">
                            <button type="submit" class="btn btn-primary">ذخیره</button>

                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
</section>

and action is :

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Setting(IEnumerable<Models.Setting> settings)
    {
        db.Entry(settings);
        db.SaveChanges();
        return View(settings);
    }

but settings is null!

2 Answers 2

3

You can't use @foreach loop in the view in order to pass list of object back to the controller, because that would generate input elements with same IDs and Names.

Instead, use standard for loop:

for(int i = 0; i < Model.Count; i++)
{
    <div class="card-body">
        <div class="form-group">
            @Html.HiddenFor(m => m[i].Id)
            <label for="exampleInputPassword1">@Model[i].Name</label>
            @Html.EditorFor(m => m[i].Value, 
                new { htmlAttributes = new { @class = "form-control"} })
            @Html.ValidationMessageFor(m => m[i].Value, 
                new { @class = "text-danger" })
        </div>
    </div>
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you! but @Model[i].Name OR @Html.DisplayFor(m => m[i].Name) don't sent value!
You didn't put the name in an input (currently you have it in a label) hence I thought you don't need it back to the server. If you want to keep the name as well, just put it in a hidden input. Remember, in order to post data back to the server, they will need to be in an input!
1

Few things going on here. The model needs all fields to be used one way or another to be recognized. Here is a sample solution

public class Song
{
    public string Name { get; set; }
    public int Id { get; set; }
    public string Artist { get; set; }
    public Genre genre { get; set; }

    public int length { get; set; }
    
}

public class Playlist
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public List<Song> Songs { get; set;}
}

public class MusicViewModel
{
    public int Id { get; set; }
    public Playlist Playlist { get; set; }
    public string Title { get; set; }
}

and then in the View, notice how I use Model, not model, this matters. As well as Model.list[i], rather than element at. Hope this helps.

@using (Html.BeginForm("Create", "Music", FormMethod.Post))
{
@Html.AntiForgeryToken()

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th></th>
    </tr>

        <tr>
            <td>
                @Html.EditorFor(model => Model.Title)
            </td>
            <td>
                @Html.EditorFor(model => Model.Id)
            </td>
        </tr>
</table>

<table class="table">
    <tr>
        <th>
            Playlist Name
        </th>
        <th>
            Description
        </th>
        <th>
            Id
        </th>
        <th>
            Songs
        </th>
    </tr>
    <tr>
        <td>
            @Html.EditorFor(model => Model.Playlist.Name)
        </td>
        <td>
            @Html.EditorFor(model => Model.Playlist.Description)
        </td>
        <td>
            @Html.EditorFor(model => Model.Playlist.Id)
        </td>
        <td>
            @*@Html.HiddenFor(model => Model.Playlist.Songs)*@
        </td>
    </tr>


</table>


<table class="table">
    <tr>
        <th>
            Name
        </th>
        <th>
            Artist
        </th>
        <th>
            Genre
        </th>
        <th>
            Length
        </th>
        <th></th>
    </tr>

    @for(int i=0; i< Model.Playlist.Songs.Count; i++)
    {
        <tr>
            <td>
                @Html.EditorFor(model => Model.Playlist.Songs[i].Name)
            </td>
            <td>
                @Html.EditorFor(model => Model.Playlist.Songs[i].Artist)
            </td>
            <td>
                @Html.EditorFor(model => Model.Playlist.Songs[i].genre)
            </td>
            <td>
                @Html.EditorFor(model => Model.Playlist.Songs[i].length)
            </td>
            <td>
                @Html.EditorFor(model => Model.Playlist.Songs[i].Id)
            </td>
        </tr>
    }

</table>
<p>
    <input type="submit" value="Create" class="btn btn-default" />
</p>

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.