I am following the example for Validation in ListView Editing but I am having a problem.
The problem is that after I Save and Update to the ListView, it is calling CleanUpValidation but then is getting back into the code in EditTemplate and is therefore setting the currEditItem and currEditContext again to the Model that I just updated.
This is causing problems if I try to add a new item after the update it thinks the new item is valid when it isn't because the currEditContext's model still has the item I just updated.
I can not figure out why it is getting back into the EditTemplate after Updating?
<
EditTemplate
>
@{
currEditItem = context;
if (currEditItem.Id == Guid.Empty)
{
currEditItem.Id = Guid.NewGuid();
currEditItem.UserId = userId;
currEditItem.FullName = GetUserName();
currEditItem.DateWorked = DateTime.Today;
currEditItem.TicketId = Ticket.Id;
}
if (currEditContext == null)
{
currEditContext = new EditContext(currEditItem);
}
<
EditForm
EditContext
=
"@currEditContext"
Context
=
"formContext"
>
<
DataAnnotationsValidator
/>
<
ValidationSummary
/>
<
div
class
=
"container-fluid editTimeEntry"
>
<
div
class
=
"row"
>
<
div
class
=
"col"
>
<
ListViewCommandButton
Command
=
"Save"
Class
=
"float-right mr-1"
Icon
=
"@IconName.Save"
Title
=
"Save"
></
ListViewCommandButton
>
<
ListViewCommandButton
Command
=
"Cancel"
Class
=
"float-right"
Icon
=
"@IconName.Cancel"
Title
=
"Cancel"
></
ListViewCommandButton
>
</
div
>
</
div
>
<
div
class
=
"row"
>
<
label
for
=
"Staff"
class
=
"font-weight-bold col-1"
>Staff</
label
>
<
div
class
=
"col"
>
@if (isAdmin)
{
<
TelerikDropDownList
@
bind-Value
=
"@currEditItem.UserId"
Data
=
"@staff"
Id
=
"Staff"
ValueField
=
"Id"
TextField
=
"FullName"
>
</
TelerikDropDownList
>
}
else
{
<
span
>@currEditItem.FullName</
span
>
}
</
div
>
</
div
>
<
div
class
=
"row"
>
<
label
for
=
"DateWorked"
class
=
"font-weight-bold col-1"
>Work Date</
label
>
<
div
class
=
"col"
>
<
TelerikDatePicker
Id
=
"DateWorked"
@
bind-Value
=
"currEditItem.DateWorked"
Format
=
"M/d/yyyy"
Max
=
"DateTime.Today"
></
TelerikDatePicker
>
</
div
>
</
div
>
<
div
class
=
"row"
>
<
label
class
=
"font-weight-bold col-1"
>Time spent</
label
>
<
div
class
=
"col-2"
>
<
label
for
=
"TimeWorked"
class
=
"mr-1"
>Time Worked</
label
>
<
span
@
ref
=
"timeWorked"
@onfocusin="(() => SelectOnFocus(timeWorked))">
<
TelerikNumericTextBox
Id
=
"TimeWorked"
Value
=
"@currEditItem.TimeWorked"
Format
=
"#0.00 hr"
Decimals
=
"2"
Step
=
".25"
ValueChanged="@( (double v) => TimeWorkedChangeHandler(v) )"
ValueExpression="@( () => currEditItem.TimeWorked )">
</
TelerikNumericTextBox
>
</
span
>
</
div
>
<
div
class
=
"col-2"
>
<
label
for
=
"BillabeTime"
class
=
"mr-1"
>Billable Time</
label
>
<
span
@
ref
=
"billableTime"
@onfocusin="(() => SelectOnFocus(billableTime))">
<
TelerikNumericTextBox
Id
=
"BillabeTime"
Value
=
"@currEditItem.BillableTimeWorked"
Format
=
"#0.00 hr"
Decimals
=
"2"
Step
=
".25"
ValueChanged="@( (double v) => BillableTimeWorkedChangeHandler(v) )"
ValueExpression="@( () => currEditItem.BillableTimeWorked )">
</
TelerikNumericTextBox
>
</
span
>
</
div
>
<
div
class
=
"col"
></
div
>
</
div
>
<
div
class
=
"row"
>
<
label
for
=
"Comments"
class
=
"font-weight-bold col-1"
>Comments</
label
>
<
div
class
=
"col"
>
<
InputTextArea
@
bind-Value
=
"currEditItem.Comments"
/>
</
div
>
</
div
>
</
div
>
</
EditForm
>
}
</
EditTemplate
>
private async Task UpdateTimeEntryHandler(ListViewCommandEventArgs args)
{
var timeEntry = (TimeEntryViewModel)args.Item;
if (!currEditContext.Validate())
{
args.IsCancelled = true;
return;
}
var dbTimeEntry = mapper.Map<
TimeEntryViewModel
, TimeEntry>(timeEntry);
var result = await ticketRepository.SaveTimeEntry(dbTimeEntry);
if (result)
{
int index = Ticket.TimeEntries.FindIndex(te => te.Id == timeEntry.Id);
if (index > -1)
{
Ticket.TimeEntries[index] = mapper.Map<
TimeEntry
, TimeEntryViewModel>(dbTimeEntry);
}
UpdateTotals();
}
CleanUpValidation();
}
private void CleanUpValidation()
{
currEditContext = null;
currEditItem = null;
}