0

I have a Kendo DateTimePicker that is not passing the changed value to the Controller. I just get the original bound value.

Here is the View Code:

@for (var i = 0; i < Model.Call.CallTimeSheets.Count(); i++ )
{

    <tr>                                            
        <td>@(Html.Kendo().DateTimePickerFor(model => Model.Call.CallTimeSheets[i].StartTime)
                                                .Name("Start" + i.ToString())
                                                .Format("dd-MM-yyyy HH:mm")
        </td>

        <td>@(Html.Kendo().DateTimePickerFor(model => Model.Call.CallTimeSheets[i].StopTime)
                                                .Name("Stop" + i.ToString())
                                                .Format("dd-MM-yyyy HH:mm")  
        </td>                                   
    </tr>  

 }

Here is the ViewModel:

public class CallEditViewModel
{
    public Call Call { get; set; }

    public IEnumerable<SelectListItem> Employees { get; set; }

    public IEnumerable<SelectListItem> ClientCallers { get; set; }

    public IList<CallTimeSheet> TimeSheet { get; set; }
}

And here is the Model:

[Table("CallTimeSheet")]
public class CallTimeSheet
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid ID { get; set; }

    public Guid CallID { get; set; }

    [ForeignKey("CallID")]
    public virtual Call Call { get; set; }

    public Guid? EmployeeID { get; set; }

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MMMMM-dd}")]
    [Display(Name = "Start Time")]
    public DateTime? StartTime { get; set; }

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MMMMM-dd}")]
    [Display(Name = "Start End")]
    public DateTime? StopTime { get; set; }

    [Timestamp]
    public byte[] RowVersion { get; set; }

    public TimeSpan? CallDuration()
    {
        return StopTime - StartTime;
    }
}

When the form submits I get no errors but the values in both the start and stop times that are posted to the controller are the original values passed to the view from the controller GET.

There are several other questions similar to this but none of the solutions have worked.

Andrew
  • 1,745
  • 1
  • 21
  • 29
  • 1
    Seems like more of a [model collection binding issue](https://stackoverflow.com/questions/14822615/how-does-mvc-4-list-model-binding-work). Your names are going to be Start1, Start2, etc. You want Start[1], Start[2], etc. – Steve Greene Jun 13 '18 at 20:46
  • What does your controller method looks like? – SJaka Jun 14 '18 at 14:05

1 Answers1

0

I did not exactly find a solution to my question but to get around my problem I instead used a simple "TextBoxFor" instead of the Kendo "DateTimePickerFor".

I changed my code to use this instead:

<td>@Html.TextBoxFor(model => Model.Call.CallTimeSheets[i].StartTime, "{0:yyyy-MM-dd 
hh:mm:ss tt}")</td>

Of this:

<td>@(Html.Kendo().DateTimePickerFor(model => Model.Call.CallTimeSheets[i].StartTime)
                                            .Name("Start" + i.ToString())
                                            .Format("dd-MM-yyyy HH:mm")</td>

Part of my issue with the name was being built incorrectly and the date format but even after correcting both in the DateTimePickerFor I was still not getting the results passed so I ended up not using it.

Andrew
  • 1,745
  • 1
  • 21
  • 29
  • DateTimePickerFor should work fine. As you have stated it's all about how the name and id attributes are being rendered. Did you try it with out the .Name()? You should not need that since you are using the `xxxFor` version of the control. That goofs up the binding because it lacks the array syntax []. Both will render inputs, but you get a far better experience with the Kendo. – Steve Greene Jun 14 '18 at 17:03