English 中文(简体)
网上每次发言
原标题:Foreach statement in .Net Core View
  • 时间:2019-07-10 17:32:42
  •  标签:
  • .net-core

我想逐项阐述在以下法典中的观点,而不是确定如何这样做。 这是我迄今为止所做的。

除此以外,我没有尝试过任何事情,而是研究过一些例子,但没有一个符合这一具体例子。

  <div class="row">
    <div class="col-md-4">
        <div class="form-group">
            <label asp-for="Packet.Meeting" class="control- 
        label">Date/Time Of Meeting:</label>
            <input asp-for="Packet.Meeting" type="date" asp-format=" 
       {0:MM/dd/yyyy}" class="form-control" />
        </div>
        @foreach (var item in Model.Item)
        {<div class="form-group">
                <label asp-for="Item.ItemTitle" class="control-label"> 
        </label>
                <input asp-for="Item.ItemTitle" class="form-control" />
            </div>}            
      </div>
      </div>

这里的模式如下:

 public class PacketViewModel
  {

    public Packet Packet { get; set; }

    public Item Item { get; set; }

    public List<Item> items { get; set; }
   }
}

I want to show all of the item titles from the item model

问题回答

只要你在意见一开始重新提及该模式,就应当罚款:

@model ProjectNameSpace.Models.PacketViewModel

现在,如果您使用<代码>foreach loop,则投入都将有相同的<代码> 姓名/代码>和<代码>id。 附录中的属性:

@foreach (var item in Model.items)
{
    <label asp-for="@item.ItemTitle" class="control-label"> </label>
    <input asp-for="@item.ItemTitle" class="form-control" />
}

因此,我们必须使用<条码>。 相反:

@for (var i = 0; i < Model.items.Count(); i++)
{
    <label asp-for="@Model.items[i].ItemTitle" class="control-label"> </label>
    <input asp-for="@Model.items[i].ItemTitle" class="form-control" />
}

使用该格式将提供独有的投入<代码>id和 属性,载于超文本。

出入证

public ActionResult Home()
{
    //create your obj and assigned all the values

   PacketViewModel list = new PacketViewModel(); //Ienumerable ModelList class defined under Model will store the list of Model data.
   // assuming you have assigned all the values to your obj

   View(list);
}

现在,在你看来,我们将明确提到这一模式。

@model Project.Models.PacketViewModel 

现在,你可以通过你的模式:

 @{

        foreach (var item in Model.Items)
        {
           //do what you want to do here for example 
           <b> @item.ItemTitle </b>
        }

  }




相关问题
Blazor Server App : Unable to unprotect the message.State

I am getting an Exception as above in my Blazor server application. Below is my Program.cs File builder.Services.AddAuthentication(AzureADB2CDefaults.BearerAuthenticationScheme) .AddOpenIdConnect(...

System Text JsonSerializer Deserialization of TimeSpan

In researching how to deserialize a TimeSpan using Newtonsoft s JSON.net I came across code in my current project that did not use Json.net. It used System.Text.Json.JsonSerializer and appeared to not ...

TCP Connection Creation and Closing Event Hooking [closed]

What helper classes C# provides to monitor all TCP connection on an OS. A piece of Sample code would also be appreciated. Note: Original intent was to monitor this on Windows but would be nice to do ...

热门标签