gpt4 book ai didi

asp.net-mvc - Kendo Grid MVC 结合了 ajax 绑定(bind)和服务器编辑

转载 作者:行者123 更新时间:2023-12-02 03:49:20 25 4
gpt4 key购买 nike

有没有办法将ajax绑定(bind)和服务器编辑结合起来?

我希望当前的操作(例如读取、分页、排序和删除)通过ajax请求来完成,并通过服务器编辑来创建和更新,因为我有一个复杂的表单需要使用整个页面。

我尝试在自定义列中插入操作链接,但它说我无法在 ajax 绑定(bind)上使用服务器编辑。

最佳答案

是的,这可以通过使用 template() 和 Kendo 客户端模板来实现。

客户端模板基本上是在运行时在客户端上执行的 JavaScript,因此我们可以传入 Kendo UI 知道的变量,这些将是您的模型属性名称。

因此,如果您希望每行旁边有一个可能链接到详细信息页面的链接,例如您会:

#=...# - Evaluates the JavaScript code expression or a string property from the data item and outputs the result in the template.
#...# - Evaluates the JavaScript code expression inside, but doesn't output value.
#:...# - Evaluates the JavaScript code expression or a string property from the data item and outputs the result in the template which is HTML encoeded.

最简单的解决方案/示例添加一个新列,如下所示:

columns.Template(x => null).ClientTemplate(Html.ActionLink("DisplayText", "Edit", new { Id = "id" }).ToHtmlString().Replace("id", "#=ClientPayeeCodeId#"));

我创建了一个 Html Helper 来帮助我实现这一目标,这样我就可以自定义 Html、集中实现等:

在 Razor View 中,我有:

columns.Template(x => null).ClientTemplate(Html.KendoActionLink("Foo", "Bar", "This is a Test", "Name",htmlAttributes: null, routeValues: new Dictionary<string, string>() { { "someParameter", "someValue" }, { "someParameter2", "someValue2" } }));

以及我的扩展方法:

        /// <summary>
/// This should be used in the Kendo UI ClientTemplate() Calls to generate MVC ActionLinks in a Kendo UI Grid
/// </summary>
/// <example>
/// Usage:
/// <code>
/// columns.Template(x => x.Foo).ClientTemplate(Html.KendoActionLink("Index", "Home", "View Foo","Foo"));
/// </code>
/// </example>
/// <param name="action">"The Action Name of a Controller you wish to call"</param>
/// <param name="controller">The Controller Name of a Controller you wish to call</param>
/// <param name="linkText">Text to display to the user</param>
/// <param name="propertyName">The property name that acts as the ID for the MVC Action</param>
/// <param name="htmlAttributes">Additonal Html attribute to add to the anchor tag</param>
/// <returns>A Relative Url string to the Action you wish to Call</returns>
public static string KendoActionLink(this HtmlHelper helper, string action, string controller, string linkText, string propertyName, IDictionary<string, string> htmlAttributes, IDictionary<string,string> routeValues)
{
//TODO: Support for MVC RouteLink (KendoRoutLink Method) and nested grids (I think) will need to use \\#= #

TagBuilder tag = new TagBuilder("a");

string kendoUrl;

//Kendo UI uses #=variableName# to escape from from text / html to JavaScript
if (!string.IsNullOrEmpty(propertyName))
{
kendoUrl = string.Format("~/{0}/{1}/#={2}#", controller, action, propertyName);
}
else
{
kendoUrl = string.Format("~/{0}/{1}", controller, action);
}

if (routeValues != null) // Adding the route values as query string, only kendo values for now
kendoUrl = kendoUrl + "?" + String.Join("&", routeValues.Select(kvp => String.Format("{0}=#={1}#", HttpUtility.UrlEncode(kvp.Key), HttpUtility.UrlEncode(kvp.Value))).ToArray());

string kendoIcon = "<span class=\"k-icon k-i-restore\"></span>";

tag.Attributes["href"] = UrlHelper.GenerateContentUrl(kendoUrl, helper.ViewContext.HttpContext);

tag.SetInnerText(kendoIcon + linkText);

if (htmlAttributes != null)
{
foreach (KeyValuePair<string, string> attribute in htmlAttributes)
{
tag.Attributes[attribute.Key] = attribute.Value;
}
}

tag.AddCssClass("k-button k-button-icontext");

return tag.ToString();
}

如果您只想在网格顶部添加一个链接,只需执行此操作即可。

.ToolBar(toolbar =>
{
toolbar.Custom().Action("Create", "SomeController").Text("<span class=\"k-icon k-i-plus\"></span> Create");
})

关于asp.net-mvc - Kendo Grid MVC 结合了 ajax 绑定(bind)和服务器编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13642040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com