将变量从cshtml剃刀传递到jquery [英] passing a variable from cshtml razor to jquery

查看:47
本文介绍了将变量从cshtml剃刀传递到jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上的局部视图如下:-

I have a partial view that I am calling on pages as follows :-

@Html.Partial("~/Views/Shared/ImageGallery.cshtml", Model)

此页面实际Jquery的代码是s:-

The code for the actual Jquery of this page is a s follows :-

<script type="text/javascript">
    $(document).ready(function () {

        $('.modal_block').click(function (e) {
            $('#tn_select').empty();
            $('.modal_part').hide();
        });

        $('#modal_link').click(function (e) {
            $('.modal_part').show();
            var context = $('#tn_select').load('/Upload/UploadImage?Page=Article&Action=Edit&id=16', function () {
                initSelect(context);
            });
            e.preventDefault();
            return false;
        });

    });
</script>

现在这可以完美工作,但是我需要找到一种方法来将动态变量而不是硬编码变量传递给此方法:-

Now this works perfectly, however I need to find a way to pass dynamic vars instead of hard coded vars to this :-

Upload/UploadImage?Page=Article&Action=Edit&id=16

在模型中,我拥有所有变量,但是我不知道如何将它们插入到Jquery中.任何帮助将不胜感激!

In the Model I have all the vars, however I do not know how I can insert them into the Jquery. Any help would be very much appreciated!

--------- UPDATE -----------------------

---------UPDATE-----------------------

这是我放入需要ImageGallery的每个cshtml中的代码.

This is the code I am putting into each cshtml that needs the ImageGallery.

</div>
    @Html.HiddenFor(model => model.PageViewModel.Page.PageTitle, new { id = "PageTitle"});
    @Html.HiddenFor(model => model.PageViewModel.Page.PageAction, new { id = "PageAction"});
    @Html.HiddenFor(model => model.ArticleViewModel.Article.ArticleID, new { id = "ArticleID"});
<div>
    @Html.Partial("~/Views/Shared/ImageGallery.cshtml", Model)
</div>

ImageGallery中的新Javascript:-

New Javascript in the ImageGallery :-

<script type="text/javascript">

    var pageTitle = $('#PageTitle').val();
    var pageAction = $('#PageAction').val();
    var id = $('#ArticleID').val();
    $(document).ready(function () {
        $('.modal_block').click(function (e) {
            $('#tn_select').empty();
            $('.modal_part').hide();
        });
        $('#modal_link').click(function (e) {
            $('.modal_part').show();
            var context = $('#tn_select').load('/Upload/UploadImage?Page=' + pageTitle + '&Action=' + pageAction + '&id=' + id, function () {
                initSelect(context);
            });
            e.preventDefault();
            return false;
        });
    });

</script>

现在可以正常使用

推荐答案

您可以将隐藏字段添加到视图中,并从模​​型绑定数据.然后,您可以轻松地从jQuery读取此值.

You can add hidden field to your view and bind data form the model. Then you can easily read this value from jQuery.

查看:

@Html.HiddenFor(model => model.Id, new { id = "FieldId"});

脚本:

var id= $('#FieldId').val();

此外,您也可以将此隐藏项放入局部视图中.如果您的部分视图不是强类型的,则将HiddenFor更改为Hidden.您的ImageGallery部分视图应包含以下div:

Also you can put this hiddens into your partial view. If your partial view is not strongly typed change HiddenFor to Hidden. Your ImageGallery partial view should contain the following div:

</div>
    @Html.Hidden("PageTitle", Model.PageViewModel.Page.PageTitle);
    @Html.Hidden("PageAction", Model.PageViewModel.Page.PageAction);
    @Html.Hidden("ArticleID", Model.ArticleViewModel.Article.ArticleID);
<div>

在这种情况下,您不需要为需要ImageGallery的每个cshtml放置隐藏内容.

In this case you don't need to put hiddens to every cshtml that needs the ImageGallery.

这篇关于将变量从cshtml剃刀传递到jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆