MVC - PartialView在View中找不到事件

时间:2015-10-08 05:39:08

标签: javascript asp.net-mvc events model-view-controller telerik

我有一个窗口,其内容通过局部视图加载。将jscript代码放入局部视图只会导致我试图将其放入包含窗口的视图中的问题。

调试此页面在DropDownList事件上加载视图时,我立即得到一个未定义的错误,其他任何事件都不起作用。我花了最后几个小时寻找解决方案,但找不到任何东西。

查看:

@model List<CrmProfileVM>

@using DAKCrmImport.Models;
@{
    ViewBag.Title = "Index";
}

<h2>Profilübersicht</h2>
@(Html.Kendo().Grid<CrmProfileVM>().Name("gvCrmProfile").BindTo(Model).Columns(c =>
        {
            c.Template(t => { }).HeaderTemplate("").ClientTemplate(@"
                <a href='javascript: void(0)' class='abutton delete' onclick='deleteRow(this)' title='Löschen'>button delete</a>
                <a href='javascript: void(0)' class='abutton edit' onclick='editRow(this)' title='Bearbeiten'>button edit</a>")
            .Width(250);
            c.Bound(i => i.Name);
            c.Bound(i => i.JobCount).Title("Jobanzahl");
            c.Bound(i => i.CrmType).Title("Typ");
            c.Bound(i => i.CreatedBy).Title("Ersteller");
            c.Bound(i => i.UserGroup).Title("Benutzergruppe");
            c.Bound(i => i.CreatedAt).Title("Erstellt am").Format("{0:dd.MM.yyyy HH:mm:ss}");
            c.Bound(i => i.LastUpdatedBy).Title("Letzte Anpassung durch");
            c.Bound(i => i.LastUpdatedAt).Title("Letzte Anpassung am").Format("{0:dd.MM.yyyy HH:mm:ss}");
        }) //"<a href='" + @Url.Action("Add", "Profile") + "' class='abutton create' title='Neues Profil anlegen'>button create</a>"
        .ToolBar(toolBar => toolBar.Template("<a href='javascript: void(0)' class='abutton create' title='Neues Profil anlegen' onclick='createProfile()'>button create</a>"))
        .HtmlAttributes(new { style = "height: 680px;" })
        .Editable(editable => editable.Mode(GridEditMode.PopUp))
        .Scrollable()
        .ClientDetailTemplateId("tpCrmProfile")
        .Groupable()
        .Sortable()
        .Pageable(pageable => pageable
        .Refresh(true)
        .PageSizes(true)
        .ButtonCount(5))
        .Filterable()
        .DataSource(dataSource => dataSource
            .Ajax()
            .ServerOperation(false)
            .Model(model => model.Id(p => p.Id))
            .Update(update => update.Action("EditingPopup_Update", "Grid"))
            .Destroy(update => update.Action("EditingPopup_Destroy", "Grid"))
            .Create("Add", "Profile")
        )
)


@(Html.Kendo().Window()
    .Name("createProfileWindow")
    .Title("Profilerstellung")
    .Content(@<text>
    </text>)
    .Draggable()
    .Resizable()
    .Width(700)
    .Height(800)
    .Visible(false)
    .Iframe(true)
    .LoadContentFrom("Add", "Profile")
    .Scrollable(false)
    .AutoFocus(true)
)
<script>

    function createProfile() {
        var window = $("#createProfileWindow").data("kendoWindow");
        window.open();
        window.center();
    }


    function showDetails() {

    }


    function onSourceDataTypeSelect(e) {
        var dataItem = this.dataItem(e.item);
        var ddlSeparator = $("#Separator").data("kendoDropDownList");

        if (dataItem.Text == "csv") {
            ddlSeparator.enable();
        } else {
            ddlSeparator.select(0);
            ddlSeparator.enable(false);
        }
    }
</script>

PartialView:

@model DAKCrmImportModel.Model.Entities.Base.CrmProfile

@{
    ViewBag.Title = "_CrmProfileAdd";
}

@(Html.Kendo().Notification().Name("gridNotification"))

@using (Html.BeginForm("Save", "Profile", FormMethod.Post, new { Id = "addProfileForm", novalidate = "false" }))
{   
        <ul id="progressbar">
                <li class="active">Grundeinstellungen</li>
                <li>CRM-Einstellungen</li>
                <li>Mapping anlegen</li>
        </ul>
        <fieldset id="fs1">
            <h2 class="fs-title">Grundinformationen</h2>
            <div class="formClearContainer">
                @(Html.ActionLink("Formular leeren", MVC.Profile.ClearForm(), new { @class = "clearForm" }))
            </div>
            @Html.LabelFor(model => model.Name, new { @class = "label req" })
            @Html.TextBoxFor(model => model.Name, new { @class = "input" })
            @Html.LabelFor(model => model.Description, new { @class = "label" })
            @Html.TextAreaFor(model => model.Description, new { @class = "input", rows = "3", cols = "25" })
            @Html.LabelFor(model => model.UserGroupId, new { @class = "label req" })
            @(Html.Kendo().DropDownListFor(model => model.UserGroupId)
                .OptionLabel("Bitte auswählen... ")
                .BindTo(Model.UserGroupList)
            .Popup(p => p.AppendTo("#fs1"))
            )
            @(Html.LabelFor(model => model.SourceDataType, new { @class ="label req" }))
            @(Html.Kendo().DropDownListFor(m => Model)
                .BindTo(EnumHelper.GetSelectList(typeof(DAKCrmImportModel.Model.Entities.Base.SourceDataType)))
                .OptionLabel("Bitte auswählen... ")
                .Name("SourceDataType")
                .Events( e=>
                    {
                        e.Select("onSourceDataTypeSelect");
                    }))
            @(Html.LabelFor(model => model.Separator, new { @class="label req" }))
            @(Html.Kendo().DropDownListFor(m => Model)
            .BindTo(EnumHelper.GetSelectList(typeof(DAKCrmImportModel.Model.Entities.Base.SeparatorType)))
                .OptionLabel("Bitte auswählen... ")
                .Name("Separator")
                .Events( e=>
                    e.Select("onSeparatorTypeSelect"))
                .Enable(false))
            <input type="button" name="next" class="next action-button" value="Weiter" style="display:block"/>
        </fieldset>
        <fieldset id="fs2">
            <h2 class="fs-title">CRM-Einstellungen</h2>
            <div class="formClearContainer">
                @(Html.ActionLink("Formular leeren", MVC.Profile.ClearForm(), new { @class = "clearForm" }))
            </div>
            @Html.LabelFor(model => model.OrdnungsbegriffTypeId, new { @class = "label req" })
            @(Html.Kendo().DropDownListFor(model => model.OrdnungsbegriffTypeId)
                .OptionLabel("Bitte auswählen...")
                .BindTo(Model.OrdnungsbegriffTypeList)
            .Popup(p => p.AppendTo("#fs2"))
            )
            @Html.LabelFor(model => model.CrmTypeId, new { @class = "label req" })
            @(Html.Kendo().DropDownListFor(model => Model.CrmTypeId)
                .Name("CrmTypeId")
                .OptionLabel("Bitte auswählen...")
                .BindTo(Model.CrmTypeList)
            .Popup(p => p.AppendTo("#fs2"))
                .Events(e =>
                    e.Select("onCrmTypeSelect"))
            )
            <br />
            <input type="button" name="previous" class="previous action-button" value="Zurueck" />
            <input type="button" name="next" class="next action-button" value="Weiter" />
        </fieldset>

        <fieldset>
            <h2 class="fs-title">Mapping anlegen</h2>
            <div class="formClearContainer">
                @(Html.ActionLink("Formular leeren", MVC.Profile.ClearForm(), new { @class = "clearForm" }))
            </div>
            @(Html.Kendo().Upload()
                .Name("upMappingFile")
                .Multiple(false)
                .ShowFileList(true)
                .TemplateId("fuCsvTemplate")
                .Async(a => a
                    .AutoUpload(true)
                    .Save("SaveFile", "Mapping")
                    .Remove("RemoveFile", "Mapping")
                )
                .Enable(false)
                .Events(e => e
                    .Success("onSuccess")
                    .Upload("onUpload"))
                .Messages(m => m
                    .Select("Durchsuchen...")
                    .HeaderStatusUploading("Uploading...")
                    .HeaderStatusUploaded("Fertig"))
            )
            <div id="gridContainer">
            </div>
            @*<div id="appendTo" class="k-block"></div>*@
            <input type="button" name="previous" class="previous action-button" value="Zurueck" />
            <input type="submit" name="submit" class="submit action-button" value="Speichern"/>
</fieldset>
}

控制器-的ActionResult:

public virtual ActionResult Add()
        {
            CrmProfile crmProfile = new CrmProfile();
            crmProfile = GetCrmProfile(crmProfile);

            return PartialView("_CrmProfileAdd", crmProfile);
        }

这不是完整的代码,但将所有内容放在这里会有点过分。

那么简单的问题:为什么部分视图在包含它的视图中找不到事件?我怎样才能优雅地解决这个问题?

先谢谢你,伙计们。 :)

[更新1] 所以在从部分视图中取出脚本参考之后,这是我必须要做的事情。当页面试图启动一个Kendo / Telerik-controlls时,我得到一个“jQuery is not defined”-error .. yaaaayy。

jQuery(function(){jQuery("#UserGroupId").kendoDropDownList({"dataSource":[{"Text":"0023 10 - VZ_RSA","Value":"1"}],"dataTextField":"Text","popup":{"appendTo":"#fs1"},"dataValueField":"Value","optionLabel":"Bitte auswählen... "});});

我再次开始研究并尝试将@section脚本放入视图并插入所有脚本。不幸的是,它没有做任何事情,是的,我的_Layout中有RenderSection部分。我真的不知道发生了什么。 Fiddler没有显示任何错误。

布局:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    @*<link rel="stylesheet" href="http://kendo.cdn.telerik.com/<kendo ui version>/styles/kendo.common-bootstrap.min.css" />
    <link rel="stylesheet" href="http://kendo.cdn.telerik.com/<kendo ui version>/styles/kendo.bootstrap.min.css" />*@
    <title>@ViewBag.Title - Meine ASP.NET-Anwendung</title>
    @Scripts.Render("~/bundles/modernizr")
    @Styles.Render("~/Content/kendo/css")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/bootstrap")
    @Scripts.Render("~/bundles/kendo")
    @Scripts.Render("~/bundles/jqueryeasing")
</head>
<body>
    <div id="wrapper">
        <header>
            <div id="logo"></div>
            <div id="logoHead">CrmImport</div>
            <div id="links">
                <a href="#">Über uns</a>
                <a href="#">Hilfe</a>
                <a href="#">Kontakt</a>
            </div>
        </header>
        <div id="breadCrumpContainer">
            <div id="breadCrump">@ViewContext.RouteData.Values["controller"]</div>
        </div>
        <nav>
            <ul id="menu">
                @* Linkname, Action, Controller *@
                <li>@Html.ActionLink("Home", MVC.Home.Index()) </li>
                <li>
                    @Html.ActionLink("Meine Profile", MVC.Profile.Index())
                </li>
                <li>
                    @Html.ActionLink("Meine Jobs", MVC.Profile.Index())
                </li>
                <li>
                    <a href="#">Administration</a>
                    <ul>
                        <li>@Html.ActionLink("Usergruppen verwalten", "Index", "UserGroups")</li>
                        <li>@Html.ActionLink("Log", "Index", "Log")</li>
                        <li>@Html.ActionLink("Globale Profile", "Index", "GlobalProfiles")</li>
                        <li>@Html.ActionLink("News verwalten", "Index", "News")</li>
                    </ul>
                </li>
                <content>
                    <div id="userBox">
                        @Html.Action("_User", "User")
                    </div>
                </content>
            </ul>
        </nav>
    </div>
    <div id="content">
        @RenderBody()
    </div>
    <div id="push"></div>
    <footer>Copyright © DAK-Gesundheit 2015</footer>
    @RenderSection("scripts", required: false)
</body>
</html>
<link href="~/Styles/Main.css" rel="stylesheet" />

1 个答案:

答案 0 :(得分:0)

最后问题是Kendo.Window或者更确切地说是加载了时间内容,这是在构建父视图时。

要解决此问题,我必须在单击特定按钮进行编辑和创建时加载Windows内容。

您可以忽略整个href-part。这是为了调试。

namespace Filing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button_Save_Click(object sender, EventArgs e)
        {

            SaveFileDialog file = new SaveFileDialog();

            file.Filter = "Text (*.txt) | Word File *.doc";
            file.Title = "Save a file";
            File.WriteAllText(file.FileName, richTextBox1.Text);

            file.ShowDialog();
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}