简体   繁体   English

将变量从cshtml razor传递给jquery

[英]passing a variable from cshtml razor to jquery

I have a partial view that I am calling on pages as follows :- 我有一个部分观点,我在页面上调用如下: -

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

The code for the actual Jquery of this page is as follows :- 此页面的实际Jquery的代码如下: -

<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

In the Model I have all the vars, however I do not know how I can insert them into the Jquery. 在模型中我有所有变量,但是我不知道如何将它们插入到Jquery中。 Any help would be very much appreciated! 任何帮助将非常感谢!

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

This is the code I am putting into each cshtml that needs the ImageGallery. 这是我在每个需要ImageGallery的cshtml中添加的代码。

</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>

New Javascript in the ImageGallery :- ImageGallery中的新Javascript: -

<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>

This works fine now 这现在工作正常

You can add hidden field to your view and bind data form the model. 您可以向视图添加隐藏字段,并将数据绑定到模型中。 Then you can easily read this value from jQuery. 然后,您可以轻松地从jQuery中读取此值。

View: 视图:

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

Script: 脚本:

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

Also you can put this hiddens into your partial view. 你也可以将这些隐藏在你的局部视图中。 If your partial view is not strongly typed change HiddenFor to Hidden. 如果您的局部视图不是强类型,请将HiddenFor更改为Hidden。 Your ImageGallery partial view should contain the following div: 您的ImageGallery局部视图应包含以下div:

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

In this case you don't need to put hiddens to every cshtml that needs the ImageGallery. 在这种情况下,您不需要将hiddens放入需要ImageGallery的每个cshtml。

You can either set hidden fields or just declare javascript variables and set their values from either your model or the Viewbag, just like: 您可以设置隐藏字段,也可以只声明javascript变量并从模型或Viewbag中设置它们的值,如:

var action = @Model.action;

or 要么

var id = @ViewBag.id;

and you can just use it in your code 你可以在你的代码中使用它

<script type="text/javascript">

var action = @Model.action;
var id = @ViewBag.id;
$(document).ready(function () {

$('.modal_block').click(function (e) {
    $('#tn_select').empty();
    $('.modal_part').hide();
});
$('#modal_link').click(function (e) {
    $('.modal_part').show();
    var urlToLoad = "/Upload/UploadImage?Page=Article&Action=" + action + "&id=" + id;
    var context = $('#tn_select').load(urlToLoad, function () {
        initSelect(context);
    });
    e.preventDefault();
    return false;
});

}); });

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

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