重复部分视图不起作用

时间:2012-09-04 08:34:44

标签: c# jquery json asp.net-mvc-3 razor

我有一个索引页面,我有一个链接来调用局部视图。代码如下: -

@model MvcCommons.ViewModels.CompositeViewModel

@{
ViewBag.Title = "Index";
}

<script src="../../Scripts/jquery.jtruncate.js" type="text/javascript"></script>


<script type="text/javascript">
$().ready(function() {
    $('.text').jTruncate({
        length: 100,
        minTrail: 0,
        moreText: "",
        lessText: "Read Less",
        //ellipsisText: " (Click below to continue reading)"
    });
});
</script>


<script type="text/javascript">
$('#myTable01').fixedHeaderTable({
    footer: false,
    cloneHeadToFoot: false,
    altClass: 'odd',
    autoShow: false
});


</script>
<table class="fancyTable" id="myTable01" cellpadding="0" cellspacing="0">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.ArticleViewModel.Article.ArticleTitle)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ArticleViewModel.Article.ArticleDate)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ArticleViewModel.Article.ArticleText)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ArticleViewModel.Article.ArticleSource)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ArticleViewModel.Article.Category.CategoryTitle)
    </th>
    <th>
        Images
    </th>
    <th></th>
</tr>

@foreach (var item in Model.ArticleViewModel.ArticleList)
{
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.ArticleTitle)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ArticleDate)
    </td>
    <td>
        <div class="text">
            @Html.DisplayFor(modelItem => item.ArticleText)
        </div>
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ArticleSource)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Category.CategoryTitle)
    </td>
    <td>
        @if (Model.ImageViewModel.ImageCount(item.ArticleID, Model.ImageViewModel.PageID) > 0)
        {
            @Html.HiddenFor(model => Model.PageViewModel.Page.PageTitle, new { id = "PageTitle" })
            @Html.HiddenFor(model => Model.PageViewModel.Page.PageAction, new { id = "PageAction" })
            @Html.HiddenFor(model => item.ArticleID, new { id = "ItemID" })
            @Html.Partial("~/Views/Shared/ViewImages.cshtml", Model)
        }
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id = item.ArticleID }) |
        @Html.ActionLink("Details", "Details", new { id = item.ArticleID }) |
        @Html.ActionLink("Delete", "Delete", new { id = item.ArticleID })
    </td>
</tr>
}
</table>

<p>
     @Html.ActionLink("Create New", "Create")


</p>

ViewImages.cshtml页面如下: -

@model MvcCommons.ViewModels.CompositeViewModel

@{

ViewBag.Title = "Modal image uploader";
}


<script type="text/javascript">
var pageTitle = $('#PageTitle').val();
var pageAction = $('#PageAction').val();
var id = $('#ItemID').val();

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

});

</script>

<div class="modal_block modal_part"></div>
<div class="modal_dialog modal_part" id="tn_view_select"></div>



<p>

<a href="#" id="modal_view_link" >Click here to view the images</a>
</p>

我的问题是,由于索引是转发器,我需要区分这些部分视图,因为目前只有第一个链接可以工作(在索引页面中)。

我认为需要做的是在ViewImages.cshtml中,这部分代码: -

<a href="#" id="modal_view_link" >Click here to view the images</a>

所有链接的id都必须是唯一的。

非常感谢任何想法。

感谢您的帮助和时间


更新 - 将所有内容移至1页,Index.cshtml,但仍无法正常工作

@model MvcCommons.ViewModels.CompositeViewModel

@{
ViewBag.Title = "Index";
}

<script src="../../Scripts/jquery.jtruncate.js" type="text/javascript"></script>

<script type="text/javascript">
var pageTitle = $('#PageTitle').val();
var pageAction = $('#PageAction').val();
var id = $('#ItemID').val();

$(document).ready(function () {
    $('.modal_block').click(function (e) {
        $('#tn_select').empty();
        $('.modal_part').hide();
    });
    $('#modal_view_link').click(function (e) {
        var id = $(this).attr("data-id");
        $('.modal_part').show();
        var context = $('#tn_view_select').load('/Upload/Index?Page=' + pageTitle + '&Action=' + pageAction + '&id=' + id, function () {
            initSelect(context);
        });
        e.preventDefault();
        return false;
    });

    $('#myTable01').fixedHeaderTable({
        footer: false,
        cloneHeadToFoot: false,
        altClass: 'odd',
        autoShow: false
    });

});

</script>


<table class="fancyTable" id="myTable01" cellpadding="0" cellspacing="0">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.ArticleViewModel.Article.ArticleTitle)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ArticleViewModel.Article.ArticleDate)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ArticleViewModel.Article.ArticleText)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ArticleViewModel.Article.ArticleSource)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ArticleViewModel.Article.Category.CategoryTitle)
    </th>
    <th>
        Images
    </th>
    <th></th>
</tr>

@{var i = 0;}

@foreach (var item in Model.ArticleViewModel.ArticleList)
{
i++;
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.ArticleTitle)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ArticleDate)
    </td>
    <td>
        <div class="text">
            @Html.DisplayFor(modelItem => item.ArticleText)
        </div>
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ArticleSource)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Category.CategoryTitle)
    </td>
    <td>
        @if (Model.ImageViewModel.ImageCount(item.ArticleID, Model.ImageViewModel.PageID) > 0)
        {
            @Html.HiddenFor(model => Model.PageViewModel.Page.PageTitle, new { id = "PageTitle" })
            @Html.HiddenFor(model => Model.PageViewModel.Page.PageAction, new { id = "PageAction" })
            @Html.HiddenFor(model => item.ArticleID, new { id = "ItemID" })
            <a href="#" class="modal_view_link" data-id="@(i)">Click here to view the images</a>
        }
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id = item.ArticleID }) |
        @Html.ActionLink("Details", "Details", new { id = item.ArticleID }) |
        @Html.ActionLink("Delete", "Delete", new { id = item.ArticleID })
    </td>
</tr>
}

</table>

<p>
@Html.ActionLink("Create New", "Create")
</p>

1 个答案:

答案 0 :(得分:0)

对于满足<script>条件的ViewImages.cshtml的每次迭代,是否有理由在Model.ArticleViewModel.ArticleList中呈现ImageCount个内容?

我将<script>内容呈现给页面一次(即:foreach之外),<a ..>从局部视图中拉出并直接放在{ {1}}。

然后您可以将foreach切换为;

foreach

然后将jQuery更新为类似的内容;

@{var articleList = Model.ArticleViewModel.ArticleList; }
@for(int i = 0;i< articleList.Count; i++) {
     ...
     @Html.HiddenFor(model => articleList[i], new { id = "ItemID" })
     <a href="#" class="modal_view_link" data-id="@(i)">Click here to view the images</a>
     ...
}