3

In my Index View for the list of of rows I put a EditorFor instead of DisplayFor for my field. I am trying to capture a Completion Date for my rows and then have the ability to Submit all the rows that have a value. The field is a DateTime and the DatePicker for the fields work however, when I select a value in any row besides the first row it will change the value in the first row and does not place any value in the respective field.

enter image description here

Then I would like to click the Submit button and have it bring in the list and look for any Modifications to the record and record the change.

In my Index.cshtml i tried the following -

@for (var i = 0; i < Model.Count(); i++ )
        { 
            @Html.EditorFor(modelItem => item.CompletionDate[i])
        }

However it does not like item.CompletionDate[i]. I receive Cannot apply index with [] to an expression of type System.DateTime?

Update:

My model

namespace AQB_MON.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

public partial class PrevMaintenanceList
{
    public int PrevMaintenanceListID { get; set; }
    public int PreventativeMaintenanceID { get; set; }
    public int DeviceTypeMaintenanceID { get; set; }
    [Display(Name = "Completion Date")]
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public DateTime? CompletionDate { get; set; }
}
Ethel Patrick
  • 885
  • 7
  • 18
  • 38
  • What does your Model look like? You might need instead `item[i].CompletionDate`. – Jasen Oct 19 '17 at 17:21
  • When I change it to `item[i],CompletionDate` I receive the same message just on the item portion instead of on the CompletionDate portion. – Ethel Patrick Oct 19 '17 at 20:41
  • What is the type of `item`? If it is a collection you can use the indexer `item[i]`. You can't do that on a property `CompletionDate` of type `DateTime?`. If your page model is a collection `IList` then each loop iteration would be `@Html.EditorFor(m => Model[i].CompletionDate)` – Jasen Oct 19 '17 at 20:59
  • I made it an `IList` For the three items it puts in 3 `CompletionDate` Edit boxes for each record. Although it allows me to pick dates for each box. – Ethel Patrick Oct 19 '17 at 21:06
  • See this answer https://stackoverflow.com/a/19965039/2030565 – Jasen Oct 19 '17 at 21:13
  • Use `EditorTemplate` for multiple input controls with model binding, ensuring that you have `@model IEnumerable` directive you can use `@Html.EditorFor(item => item[i].CompletionDate)`. Note that indexed properties can only be used by collection properties, not structs. – Tetsuya Yamamoto Oct 20 '17 at 01:48
  • I researched it however I am not sure how to implement the `EditorTemplate` in an `Index` view. Can you point me to an example? – Ethel Patrick Oct 20 '17 at 17:29

0 Answers0