Ticket Q392014
Visible to All Users

GridView - How to specify custom EditFormTemplate / SetEditFormTemplateContent from Controller

created 13 years ago

How to accomplish the same like
s.SetEditFormTemplateContent(c =>
{
    @Html.RenderAction("LevelLimits", c.DataItem);
});
but from controller's code?

Show previous comments (2)
TL TL
Theodoros Loucaides 13 years ago

    Of course, I can use SetEditFormTemplateContent(string content) method, but how can I get access to a DataItem inside?

    DevExpress Support Team 13 years ago

      Hi Theodoros,
      It is a common approach to define markup in a View, not in a Controller. This will alow you separate the design and logic.
      From what I gather, you wish to define different extension settings for each page or user. If so, simply adjust these settings based on ViewData. Then you can populateViewData in a Controller based on a current Action. This is a common approach for MVC. This approach is used more frequently than defining markup in a Controller.
      Please take a look at the following demos to learn how to accomplish this task:
      HTML Editor - Features
      TreeView - Features
      Round Panel - Features
      Thanks,
      Marion

      TL TL
      Theodoros Loucaides 13 years ago

        Hi Marion,
        I'm very sorry, but you answered something different that I asked. Let have a look on your documentation:
        http://documentation.devexpress.com/#AspNet/CustomDocument8161
        ===
        Using a Settings Delegate within a View
        You can use a lambda expression, which is just a very concise way to construct the settings tree. This approach is compact and is well suited for customizing settings directly within a View.
        Passing a Settings Object from a Controller
        You can create and initialize a settings object within a Controller and pass this object to a View. This approach requires more coding. However, it might be helpful when it is required to pass the same settings to different Views. For instance, you might want to have a Menu with the same look, behavior and item hierarchy within more than one View.
        ====
        I was talking about second approach. If it's possible to specify EditFormTemplate within a View it should be possible to define it within a Controller as well, otherwise your documentation misled the users.
        Regards,

        Answers approved by DevExpress Support

        created 13 years ago (modified 12 years ago)

        Hello Theodoros,
        Thank you for the detailed information about your issue. To implement the required functionality, you may pass an HtmlHelper object to some static method that creates GridView settings with templates. Usually, Extension approaches are used in this scenario.
        I have modified the GridView - Inline Editing (Templates) demo to demonstrate the required results.
        At first, you may create some class to get GridView settings shared between Views.

        C#
        public static class GridViewHelper { public static GridViewSettings GetInlineEditingSettings(this HtmlHelper htmlHelper){ GridViewSettings settings = new GridViewSettings(); settings.Name = "gvEditingWithTemplate"; settings.KeyFieldName = "ProductID"; ... settings.SetEditFormTemplateContent(c => { var product = htmlHelper.ViewData["EditableProduct"] != null ? htmlHelper.ViewData["EditableProduct"] : c.DataItem; htmlHelper.ViewContext.Writer.Write( "<div class=\"edit_form\">" + "<div class=\"line\">" ); htmlHelper.DevExpress().Label( edtSettings => { edtSettings.ControlStyle.CssClass = "label"; edtSettings.Text = "Product Name:"; edtSettings.AssociatedControlName = "ProductName"; }).Render(); htmlHelper.DevExpress().TextBox( edtSettings => { edtSettings.Name = "ProductName"; edtSettings.ControlStyle.CssClass = "editor"; edtSettings.ShowModelErrors = true; }).Bind(DataBinder.Eval(product, "ProductName")).Render(); htmlHelper.ViewContext.Writer.Write( "</div>" + "<div class=\"line\">" ); ... }); return settings; } }

        After writing methods to get the settings, you may use this class in various Partial Views as your needs dictate.

        C#
        @{ var grid = Html.DevExpress().GridView(Html.GetInlineEditingSettings()); if(ViewData["EditError"] != null){ grid.SetEditErrorText((string)ViewData["EditError"]); } } @grid.Bind(Model).GetHtml()

        I have also created a new suggestion to improve our documentation and describe this approach with code samples: Documentation - Describe the process of initializing a setting object with a template within a Controller and passing this object to a View. You will be automatically notified when the suggestion status has been changed.
        Updated by Mike (DevExpress Support):
        We have described this solution in greater detail in our documentation at:
        Templates
        The main idea is to implement the extention method for the Html helper in order to access it in the Controller (not a View) scope.

          Show previous comments (3)

            Hello,
            We have described this solution in greater detail in our documentation at:
            Templates
            The main idea is to implement the extention method for the Html helper in order to access it in the Controller (not a View) scope.
            If you need further assistance with this subject, please create a new ticket in our Support Center and share your results with us.
            We will do our best to put you on the right track.

              Is this approach of initializing SetDataItemTemplateContent
              through server side still works? I tried this, and there is no definition of DevExpress() in HtmlHelper  so, you can't do this: htmlHelper.DevExpress().Label I also tried, this solution, https://www.devexpress.com/Support/Center/Question/Details/KA18612 as well… But it didn't work.

              Any other approach out there to achieve this functionality?

              DevExpress Support Team 8 years ago

                Hello Ajit,

                I've created a separate ticket on your behalf (T473301: GridView - How to specify DataItemTemplate from Controller). It has been placed in our processing queue and will be answered shortly.

                Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

                Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.