使用 C# MVC 将值列表放入单独的文本框中

本文关键字:单独 文本 MVC 列表 使用 | 更新日期: 2023-09-27 18:34:55

我在列中["aaa","bbb","ccc"]了一个值。我必须将每个值放在单独的文本框中,不带双引号。

我已经尝试过(在视图中(:

 <td>@Html.TextBoxFor(m => (m.editPeriodicTask.args[0]), new { @id = "args0", @class = "form-control-list" })</td>
  <td>@Html.TextBoxFor(m => (m.editPeriodicTask.args[1]), new { @id = "args1", @class = "form-control-list" })</td>
 <td>@Html.TextBoxFor(m => (m.editPeriodicTask.args[2]), new { @id = "args2", @class = "form-control-list" })</td>

还有

 <td>@Html.TextBoxFor(m => (m.editPeriodicTask.args.split(',')[0]), new { @id = "args0", @class = "form-control-list" })</td>
 <td>@Html.TextBoxFor(m => (m.editPeriodicTask.args.split(',')[1]), new { @id = "args1", @class = "form-control-list" })</td>
 <td>@Html.TextBoxFor(m => (m.editPeriodicTask.args.split(',')[2]), new { @id = "args2", @class = "form-control-list" })</td>

但它并没有给出我需要的确切解决方案。怎么做。?

使用 C# MVC 将值列表放入单独的文本框中

您也可以这样做,就像下面的代码一样

    var value = ["aaa","bbb","ccc"];
    var data1 = value.Replace("[","").Replace("]","");
    var item = data1.Split(',');
    for(var i = 0; i < item.length; i++)
    {
        var data = item[i].Replace("'"","");
        <td>@Html.TextBoxFor(m => m.editPeriodicTask.args, new { @id = "args" + i, @class = "form-control-list", @Value =  data})</td>
    }

希望这有帮助

试试这个

@{ 
     foreach (var value in Model.editPeriodicTask.args.ToList().Split(','))
     {
         <td> @Html.TextBox("name", value, new {@id = "args", @class = "form-control-list"}) </td>
     }
}

试试这个(在循环创建textboxex之前,获取一个用于区分id的计数器(

 @{int count = 0;}
 @foreach (var value in Model.editPeriodicTask.args.ToList().Split(','))
 {
    <td> @Html.TextBox("name", value, new {@id = "args"+count, @class = "form-control-list"}) </td>
count++
 }