简体   繁体   English

使用模式模板和JavaScript时如何将数据传递到MVC控制器

[英]How do i pass data to mvc controller when using a modal template and javascript

Im using telerik's Kendo UI for MVC and im trying to pass data from js to a controller. 我将Telerik的Kendo UI用于MVC,并试图将数据从js传递到控制器。

What i want to achieve is to have a modal popup asking to confirm deletion of an object (rather than navigating to a new page). 我要实现的是有一个模式弹出窗口,要求确认删除一个对象(而不是导航到新页面)。

I manage to get the popup working using demo code so its literally copy paste at this point and can be found here http://demos.telerik.com/aspnet-mvc/grid/custom-command 我设法使用演示代码使弹出窗口正常工作,因此此时可以按字面意义复制粘贴内容,并且可以在此处找到http://demos.telerik.com/aspnet-mvc/grid/custom-command

However, i am having an issue passing the ID to the controller. 但是,我在将ID传递给控制器​​时遇到问题。

<script type="text/x-kendo-template" id="template">
    <div id="details-container">
        <h2> </h2>
        <div class="container-fluid">
            <h2>
                Are you sure you want to delete <code>#= PropertyName #</code>?
            </h2>
            <hr />

                                     //// What do i pass here?!?!     VVVV
            @using (Html.BeginForm("Delete", "Properties", new { id = ????}))
            {
                @Html.AntiForgeryToken()

                <div class="form-actions no-color text-center">
                    @Html.ActionLink("Back", "Properties", "Home", null, new { type = "button", @class = "btn btn-default" })
                    <input type="submit" value="Delete" class="btn btn-danger" />
                </div>
            }
        </div>
    </div>
</script>

E: E:

i just learned that the #= PropertyName # syntax is specific to kendoUI, and that i can also use the jQuery syntax ${PropertyName} 我刚刚了解到#= PropertyName #语法特定于kendoUI,并且我还可以使用jQuery语法${PropertyName}

A hidden field for whatever data you want to send, with a name that matches the controller action method's parameter name, should suffice: 您想要发送的任何数据的隐藏字段,其名称应与控制器操作方法的参数名称相匹配,应足够:

Controller: 控制器:

public ActionResult Test(string propertyName){ //... }

View: 视图:

@using (Html.BeginForm("Delete", "Properties", new { id = ????}))
            {
                @Html.AntiForgeryToken()
                @Html.Hidden("propertyName", "Value to send goes here")
                <div class="form-actions no-color text-center">
                    @Html.ActionLink("Back", "Properties", "Home", null, new { type = "button", @class = "btn btn-default" })
                    <input type="submit" value="Delete" class="btn btn-danger" />
                </div>
            }

It doesn't need to be a string, but that is the simplest example to show as everything has to be parsed out of a string by the Default Model Binder anyway. 它不需要是字符串,但这是显示的最简单的示例,因为无论如何,都必须由Default Model Binder从字符串中解析出所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM