Articles in this section
Category / Section

How render ejControls in the Edit Form Template of the ASP.NET MVC DataGrid?

2 mins read

Grid supports several Editors of Syncfusion controls (DatePicker, DateTimePicker and DropDownList) to be rendered in the Editform using the jsrender concept. But MVC controls of the Syncfusion cannot be placed in the edit form using the jsrender concept. However, we have placed the MVC controls within the editform using the partial view concept of the MVC.

 

Razor

 

Template rendering in edit form is supported with the InlineFormTemplate and ExternalFormTemplate. To enable these modes, you must mention the EditMode as InlineFormTemplate or ExternalFormTemplate along with the template ID as shown in the following code example.

 

@(Html.EJ().Grid<object>("Grid")
        .Datasource(ds => ds.Json(ViewBag.datasource)
            .UpdateURL("/Home/Update")
            .InsertURL("/Home/Insert")
            .RemoveURL("/Home/Delete")
            .Adaptor(AdaptorType.RemoteSaveAdaptor))
        .AllowPaging()
        .PageSettings(page => page.PageSize(5))
        .EditSettings(edit =>
        {
            edit.AllowAdding()
                .AllowEditing()
                .AllowDeleting()
                //Supported EditModes
                // 1) InlineFormTemplate
                // 2) ExternalFormTemplate
                .EditMode(EditMode.InlineFormTemplate)
                .InlineFormTemplateID("template");
        })
        .ToolbarSettings(tools => {
            tools.ShowToolbar()
                .ToolbarItems(items =>
                {
                    items.AddTool(ToolBarItems.Add);
                    items.AddTool(ToolBarItems.Edit);
                    items.AddTool(ToolBarItems.Delete);
                    items.AddTool(ToolBarItems.Update);
                    items.AddTool(ToolBarItems.Cancel);
                });
        })
        .Columns(col =>
            {
                col.Field("OrderID").IsPrimaryKey(true).HeaderText("Order ID").Add();
                col.Field("CustomerID").HeaderText("CustomerID").Add();
                col.Field("OrderDate").HeaderText("Order Date").Format("{0:MM/dd/yyyy}").Add();
                col.Field("EmployeeID").HeaderText("Employee ID").Add();
            })
        .ClientSideEvents(events =>
            {
                events.ActionComplete("actionComplete");
                events.ActionBegin("actionBegin");
            })
)

 

 

Editing Template – EditPage.cshtml

 

In the EditPage.cshtml (a partial view), place the Syncfusion MVC controls as a template to the Edit Form.

 

@model MvcApplication66.models.EditableOrder
 
    <div id="template">
        <b>Order Details</b>
        <table cellspacing="10">
            <tr>
                <td style="text-align: right;">
                    Order ID
                <td style="text-align: left">
                    @Html.EJ().AutocompleteFor(o => o.OrderID).CssClass("e-field valid")
                </td>
                <td style="text-align: right;">
                    Customer ID
                </td>
                <td style="text-align: left">
                    @Html.EJ().AutocompleteFor(o => o.CustomerID).CssClass("e-field valid")
                </td>
            </tr>
            <tr>
                <td style="text-align: right;">
                    Freight
                </td>
                <td style="text-align: left">
                    @Html.EJ().NumericTextBoxFor(o => o.Freight)
                </td>
                <td style="text-align: right;">
                    Employee ID
                </td>
                <td style="text-align: left">
                    @Html.EJ().DropDownListFor(o => o.EmployeeID, (Syncfusion.JavaScript.Models.DropDownListProperties)ViewBag.ddl)
                </td>
            </tr>
            <tr>
                <td style="text-align: right;">
                    Order Date
                </td>
                <td style="text-align: left">
                    @Html.EJ().DatePickerFor(o=>o.OrderDate).CssClass("e-field")
                </td>
            </tr>
        </table>
    </div>
 
@Html.EJ().ScriptManager()@*To render Syncfusion controls in partialview*@

 

Please make a note that the Partial View page is having the ScriptManager to render the Syncfusion Controls. Since we have rendered the controls in the non-unobtrusive mode, we have mentioned this.

 

We have already discussed about rendering Syncfusion Controls in the Partial Views in following Help Document.

 

https://help.syncfusion.com/aspnetmvc/grid/how-to#work-with-partial-views

 

While editing the record we need to pass the primaryKey value of that corresponding record as a query string in the ActionComplete event of grid and call EditPartial method in the Home controller with these details through Ajax call within the same ActionComplete event for rendering the Syncfusion MVC controls in the Edit Form of the Grid after the Ajax success call.

 

 

<script>
    function actionBegin(args) {
        if (args.requestType == "save")
            this.element.ejWaitingPopup("show");
    }
    function actionComplete(args) {
        var type = args.requestType;
        if (type == "beginedit" || type == "add") {
            if (!ej.isNullOrUndefined(args.rowIndex))
                var OrderID = this.model.currentViewData[args.rowIndex]["OrderID"];
            this.element.ejWaitingPopup("show");
            $.ajax
                ({
                    url: type == "add" ? "/Home/EditPartial" : "/Home/EditPartial?OrderID=" + OrderID,
                    type: 'GET',
                    success: ej.proxy(function (data) {
                        $("#" + this._id + "EditForm").html(data);
                        if (type != "add") $("#OrderID").addClass("e-disable").attr("disabled", "disabled");
                        this.element.ejWaitingPopup("hide");
                    },this)
                });
        }
        if (args.requestType == "save")
            this.element.ejWaitingPopup("hide");
    }
</script>

 

 

Controller

EditPartial Method will handle the Ajax handler of the MVC control rendering and it will receive the ID of the respective editing record using the QueryString. Based on the PrimaryKey value, records were pulled from the server and later assigned to the MVC controls using the MVC model concept.

 

    public class HomeController : Controller
    {
        public ActionResult Index(){
            ViewBag.datasource = OrderRepository.GetAllRecords();
            return View();
        }
        [HttpGet]
        public ActionResult EditPartial()
        {
            var DataSource = OrderRepository.GetAllRecords();
            var id = 0;
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            if (Request.QueryString["OrderID"] != null)
                id = int.Parse(Request.QueryString["OrderID"]);
            var data = DataSource.Where(ID => ID.OrderID == id).SingleOrDefault();
            DropDownListProperties ddl = new DropDownListProperties();
            //dropdown(in editform) dataSource
            ddl.DataSource = list;
            ViewData["ddl"] = ddl;
            return PartialView("EditPage", data);
        }
    }

 

 

The following screenshot shows the Grid with Edit form rendered from the partial view and Syncfusion MVC Controls.

 

Figure: Grid with the inline form template holding MVC controls.  

Conclusion
I hope you enjoyed learning about how render ejControls in the Edit Form Template of the ASP.NET MVC DataGrid.

You can refer to our ASP.NET MVC Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
You can also explore our 
ASP.NET MVC Grid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied