Попытка использовать параметры с пользовательским MvcHtmlString AjaxHelper - выдает ошибку, которую невозможно преобразовать из нанизывать' - PullRequest
0 голосов
/ 08 февраля 2020

Я использую программу, которая уже имеет встроенный Custom Helper. Я хочу иметь возможность добавить параметр к этому, как показано ниже:

@Ajax.ModalDialogActionLink("Edit", "Edit", "Edit", "btn btn-warning btn-sm", new { UserName = item.UserName })

Это дает мне ошибку. Это работает с обычным ActionLink. Я считаю, что это может потребовать изменений в пользовательском помощнике. Я никогда не делал этого раньше и не знаю, с чего начать. Я вижу основы этого, но немного не знаю, что добавить или изменить.

Ниже пользовательский помощник.

public static MvcHtmlString ModalDialogActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string dialogTitle, string CssClass)
{
    var dialogDivId = Guid.NewGuid().ToString();

    return ajaxHelper.ActionLink(linkText, actionName, routeValues: null,
            ajaxOptions: new AjaxOptions
            {
                UpdateTargetId = dialogDivId,
                InsertionMode = InsertionMode.Replace,
                HttpMethod = "GET",
                OnBegin = string.Format(CultureInfo.InvariantCulture, "prepareModalDialog('{0}')", dialogDivId),
                OnFailure = string.Format(CultureInfo.InvariantCulture, "clearModalDialog('{0}');alert('Ajax call failed')", dialogDivId),
                OnSuccess = string.Format(CultureInfo.InvariantCulture, "openModalDialog('{0}', '{1}')", dialogDivId, dialogTitle)
            }, htmlAttributes: new { @class = CssClass });

}

public static MvcHtmlString ModalDialogActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string ControllerName, string dialogTitle, string CssClass)
{
    var dialogDivId = Guid.NewGuid().ToString();

    return ajaxHelper.ActionLink(linkText, actionName, ControllerName, routeValues: null,
            ajaxOptions: new AjaxOptions
            {
                UpdateTargetId = dialogDivId,
                InsertionMode = InsertionMode.Replace,
                HttpMethod = "GET",
                OnBegin = string.Format(CultureInfo.InvariantCulture, "prepareModalDialog('{0}')", dialogDivId),
                OnFailure = string.Format(CultureInfo.InvariantCulture, "clearModalDialog('{0}');alert('Ajax call failed')", dialogDivId),
                OnSuccess = string.Format(CultureInfo.InvariantCulture, "openModalDialog('{0}', '{1}')", dialogDivId, dialogTitle)
            }, htmlAttributes: new { @class = CssClass });
}

Обновление: я понял это. Для тех, кто хочет использовать код и иметь эту возможность. Я просто добавил еще 2 с нужными мне изменениями. Так что теперь есть 4 для обработки - с / без / Controller / Object: ноль и с или без / Object. Код ниже. Два выше остаются неизменными, и они были добавлены ..

        public static MvcHtmlString ModalDialogActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string dialogTitle, string CssClass, object routeValues)
    {
        var dialogDivId = Guid.NewGuid().ToString();

        return ajaxHelper.ActionLink(linkText, actionName, routeValues,
                ajaxOptions: new AjaxOptions
                {
                    UpdateTargetId = dialogDivId,
                    InsertionMode = InsertionMode.Replace,
                    HttpMethod = "GET",
                    OnBegin = string.Format(CultureInfo.InvariantCulture, "prepareModalDialog('{0}')", dialogDivId),
                    OnFailure = string.Format(CultureInfo.InvariantCulture, "clearModalDialog('{0}');alert('Ajax call failed')", dialogDivId),
                    OnSuccess = string.Format(CultureInfo.InvariantCulture, "openModalDialog('{0}', '{1}')", dialogDivId, dialogTitle)
                }, htmlAttributes: new { @class = CssClass });

    }

    public static MvcHtmlString ModalDialogActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string ControllerName, string dialogTitle, string CssClass, object routeValues)
    {
        var dialogDivId = Guid.NewGuid().ToString();

        return ajaxHelper.ActionLink(linkText, actionName, ControllerName, routeValues,
                ajaxOptions: new AjaxOptions
                {
                    UpdateTargetId = dialogDivId,
                    InsertionMode = InsertionMode.Replace,
                    HttpMethod = "GET",
                    OnBegin = string.Format(CultureInfo.InvariantCulture, "prepareModalDialog('{0}')", dialogDivId),
                    OnFailure = string.Format(CultureInfo.InvariantCulture, "clearModalDialog('{0}');alert('Ajax call failed')", dialogDivId),
                    OnSuccess = string.Format(CultureInfo.InvariantCulture, "openModalDialog('{0}', '{1}')", dialogDivId, dialogTitle)
                }, htmlAttributes: new { @class = CssClass });
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...