是否可以使用方法呈现 MVC 控件



我们正在开发一个MVC应用程序。根据要求,我们需要使用方法填充 VIEW。我创建了一个示例应用程序。您可以在"控制器"文件夹下的类中看到用于生成控件的方法Helper.cs。当我们直接将@Html.TextBox("SampleTextBox")代码放置在视图中时,它会正确呈现。但是当我们使用方法生成相同的代码时,它没有正确呈现,并且显示为纯字符串。 如果有人对此有任何想法,请告诉我们,这将非常有帮助。
示例代码

我们需要使用方法填充它,而不是直接将以下代码添加到视图中。

@Html.TextBox("SampleTextBox")

那是一些类似的东西

@Html.Raw(Helper.GetStringCode())

方法:

public static string GetStringCode()
{
return "@Html.TextBox("SampleTextBox")";
}

您可以在共享文件夹中为 Kendo 网格添加分部视图,并添加返回分部视图的 Html 帮助程序扩展:

部分视图Shared/_KendoGrid.cshtml

@model CustomViewModel
@(Html.Kendo().Grid()...)

Html 帮助程序方法:

public static MvcHtmlString KendoGrid(this HtmlHelper helper, string header)
{
return helper.Partial("Shared/_KendoGrid", new CustomViewModel { Header = header });
}

在您的.cshtml

@Html.KendoGrid("Custom header")

最新更新