How to accomplish the same like
s.SetEditFormTemplateContent(c =>
{
@Html.RenderAction("LevelLimits", c.DataItem);
});
but from controller's code?
GridView - How to specify custom EditFormTemplate / SetEditFormTemplateContent from Controller
Answers approved by DevExpress Support
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.
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?
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.
Hello Theodoros,
Please refer to the How to specify GridViewSettings on Controller side? thread to learn more on how to specify the GridView's Templates on a Controller side.
Usually View entries of an MVC application are defined within corresponding layout files. Would you please clarify why it is necessary to specify the EditFormTemplate on a Controller side?
Regards,
Mike
Hello Mike, because the view is shared and settings can be different in different cases. Sometimes it will be inline-editing and we don't need editing form at all, sometimes with different editing forms. This is the reason why we want to have possibility specify EditFormTemplate on a controller side.
Of course, I can use SetEditFormTemplateContent(string content) method, but how can I get access to a DataItem inside?
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
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,