aspnetmvc-115109-components-card-view-data-editing-and-validation-data-editing.md
The ASP.NET MVC CardView provides a built-in data editing functionality. This topic describes how to enable data editing operations within the CardView.
Note
To follow the steps described in this topic, you will need a data-bound CardView extension in your project.
You can enable data editing operations within your CardView in the following way.
Add the controller action method that will add new records to a data source.
Add the controller action method that will save the updated records to a data source.
Add the controller action method that will delete existing records from a data source.
Define the callback route values within the PartialView.
Add the command layout item and the required buttons.
CardView provides a full-featured client-side API that allows you to manipulate the edit form via JS code. The table below lists the available data editing-related methods.
| Method | Description |
|---|---|
| ASPxClientCardView.AddNewCard (via MVCxClientCardView .AddNewCard ) | Adds a new card. |
| ASPxClientCardView.StartEditCard (via MVCxClientCardView .StartEditCard ) | Opens an edit form for editing a specified row. |
| ASPxClientCardView.StartEditCardByKey (via MVCxClientCardView .StartEditCardByKey ) | Opens an edit form for editing a row with the specified key. |
| ASPxClientCardView.SetEditValue (via MVCxClientCardView .SetEditValue ) | Sets the value of the specified edit cell. |
| ASPxClientCardView.IsNewCardEditing (via MVCxClientCardView .IsNewCardEditing ) | Indicates whether or not a new card is being edited. |
| ASPxClientCardView.IsEditing (via MVCxClientCardView .IsEditing ) | Indicates whether or not the CardView extension is in edit mode. |
| ASPxClientCardView.UpdateEdit (via MVCxClientCardView .UpdateEdit ) | Saves all changes made and switches the CardView extension to browse mode. |
| ASPxClientCardView.CancelEdit (via MVCxClientCardView .CancelEdit ) | Cancels all changes made and switches the CardView extension to browse mode. |
| ASPxClientCardView.DeleteCard (via MVCxClientCardView .DeleteCard ) | Deletes the specified card. |
| ASPxClientCardView.DeleteCardByKey (via MVCxClientCardView .DeleteCardByKey ) | Deletes a card with the specified key value. |
Example
In this example, the ASPxClientCardView.CardDblClick (via MVCxClientCardView .CardDblClick ) client-side event is handled to switch the CardView to edit mode by double-clicking a data row. The event handler calls the ASPxClientCardView.StartEditCard (via MVCxClientCardView .StartEditCard ) method. This method receives the visible index of the double-clicked card and invokes an edit form.
@{
var CardView = Html.DevExpress().CardView(settings => {
settings.Name = "CardView";
settings.CallbackRouteValues = new { Controller = "Home", Action = "CardViewPartial" };
settings.SettingsEditing.AddNewCardRouteValues = new { Controller = "Home", Action = "CardViewPartialAddNew" };
settings.SettingsEditing.UpdateCardRouteValues = new { Controller = "Home", Action = "CardViewPartialUpdate" };
settings.SettingsEditing.DeleteCardRouteValues = new { Controller = "Home", Action = "CardViewPartialDelete" };
settings.CardLayoutProperties.Items.AddCommandItem(i => {
i.ShowNewButton = true;
i.ShowDeleteButton = true;
i.ShowEditButton = true;
});
settings.KeyFieldName = "CustomerID";
settings.Columns.Add("CompanyName");
settings.Columns.Add("ContactName");
settings.Columns.Add("ContactTitle");
settings.Columns.Add("Phone");
// When an end-user double-clicks a CardView card, the edit form appears, allowing an end-user to edit the card.
settings.ClientSideEvents.CardDblClick = "function(s, e) { s.StartEditCard(e.visibleIndex); }";
});
if (ViewBag.EditError != null)
{
CardView.SetEditErrorText(ViewBag.EditError);
}
}
@CardView.Bind(Model).GetHtml()