简体   繁体   中英

How to update List of string

I'm doing a course on PluralSight but the code in the course is not complete and there is a quickedit IActionResult that I can't seem to figure out.

In my View I have the code below:

@model IList<string>
@if (Model.Count > 0)
{
    <form asp-action="QuickEdit" method="post">
        @for (var i = 0; i < Model.Count; i++)
        {
        <div class="form-group">
            <label>Soup name @(i + 1);</label>
            <input id="soupNames" name="soupNames" asp-for="@Model[i]" class="form-control"/>
        </div>
        }
    <button type="submit" class="btn btn-primary">Update</button>
</form>
}
else
{
    <h2>No Soups in the system</h2>
}

The Controller:

public IActionResult QuickEdit()
{
    var soupNames = _soupRepository.AllSoups.Select(s => s.SoupName).ToList();
    return View(soupNames);
}

[HttpPost]
public IActionResult QuickEdit(List<string> soups)
{
    var soupNames = _soupRepository.AllSoups.Select(s => s.SoupName).ToList();


    for (var i = 0; i < soupNames.Count; i++)
    {
        soupNames[i] = soups[i];
     }
    return View(soups);
}

Could someone help me out? I want the values passed through replace the original values when I click the update button.

this line

<input id="soupNames" name="soupNames" asp-for="@Model[i]" class="form-control"/>

need to change to something like

<input id="@(i)_soups" name="[@(i)].soups" asp-for="@Model[i]" class="form-control"/>

you need to google model binding lists

try , it may show you the raw syntax so you can change to just input

  @Html.TextBoxFor(m => @Model[i] )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM