【问题标题】:use a jsrender tag to supply routevalue in @Url.Action()使用 jsrender 标签在 @Url.Action() 中提供路由值
【发布时间】:2016-03-24 05:51:35
【问题描述】:

我正在开发一个使用 jsrender 的 MVC 项目。 这是我第一次使用 jsrender(事实上我在 javascript 和 C# 方面还很陌生,哈哈),而且我已经为一个特定的问题苦苦挣扎了一整天。

我有以下 javascript

 $.post('@Url.Action("GetDocs","Place", new { placeID = Model.PlaceId})').done(
        function (data) {
            var template = $.templates("#doc-tmpl");
            var htmlOut = template.render(data.documents);
            $("#documentListContainer").append(htmlOut);
            $(".docCount").html(data.count);
        });

这将获取我需要的所有数据,但我需要使用其中的一部分数据创建一个 Url.Action()。

<script id="doc-tmpl" type="text/x-jsrender">
<div class="col-md-4">
        <h5>{{:Name}}{{:test}}</h5>
        <p id="DocId" class="hidden"><br />{{:DocId}}</p>
        <img src="data:image;base64,{{:FrontCoverImg}}" width="140" height="230" />
    <a href="@Url.Action("DisplayPDF","Place", new { DocId = "{{:DocId}}" })">Link</a>
</div>

我需要从@Url.Action("DisplayPDF","Place", new { docId = "{{:docId}}" }) 的 routeValues 中的数据中提供 docId。 显然,这在当前状态下不起作用。

我查看了 jsrender 文档和其他与 jsrender 和 MVC 相关的问答,但我无法理解它的工作方式。

我想到的一件事是在 javascript 中创建 @Url.Action() 并使用标签将其弹出到模板中,但是我无法弄清楚如何从$.post 以便为其提供标签。

编辑: 我的意思是,javascript/getDocs 帖子返回的数据类似于:

  documents": [
    {
        "DocId": "86f86a32-5005-456c-8dd1-c023a66dd794",
        "Name": "How to...",
        "FrontCoverImg": "Base64String(docImg)"
    },

    {
        "DocId": "d29f8afc-3191-47f1-9b88-1de08582ba27",
        "Name": "A Document",
        "FrontCoverImg": "Base64String(docImg)"
    }
 ],
"count": ​2
 }

然后设置为填写模板。我希望有一种方法可以将 @Url.Action() 语句添加到这些键/值对中,例如:

"viewdoc" : '@@Url.Action("DisplayPDF", "Place", new {DocId = ' + DocId + ', @@class = "btn btn-default" })';

(不确定语法是否完全正确)所以我可以将{{:viewdoc}} 放入模板中。

我至少在正确的轨道上吗?哈哈

【问题讨论】:

  • 你想要&lt;a href='@Url.Action("DisplayPDF","Place", new { docId = "{{:DocId}}" })'&gt;Link&lt;/a&gt;吗? (我将" 更改为' 并将docId 更改为DocId,因为您在上面的其他地方有DocId
  • 是的,应该是 DocId @BorisMoore

标签: c# json asp.net-mvc jsrender url.action


【解决方案1】:

试试这个,

 var id = $('#docId').val();
 var link = '@URL.Action("download file", "download", new { id = "-1" })';
 link = link.replace("-1", id);

来源:How to access javascript variable within @URL.Action()

【讨论】:

  • 我看到了问答,但不确定如何在我的情况下应用它。也许我只需要睡觉,哈哈。如何将链接放入模板的 html 部分?
【解决方案2】:

好的,在将它搁置一周之后,经过大量的实验和谷歌搜索,我找到了解决方案。

我在控制器中创建了链接,其中设置了 JSON 数据。 我必须请求 URL 的最左侧部分并对 url 的控制器/操作部分进行硬编码,否则它会将新 url 塞满当前页面 url 的末尾。 所以解决方法如下:

控制者:

foreach (Doc document in docs)
            {
                DocJson dj = new DocJson();
                dj.DocId = document.DocId;
                dj.Name = document.Comments;
                //get base Url
                var request = HttpContext.Request.Url.GetLeftPart(UriPartial.Authority);
                dj.docLink = request + "/Place/DisplayPDF?DocId=" + document.DocId;
                //get image bytes
                var docImg = document.FrontCoverImg;
                dj.FrontCoverImg = Convert.ToBase64String(docImg);

                documents.Add(dj);
            }
return Json(new { documents = documents, count = documents.Count() }, JsonRequestBehavior.AllowGet);

(在视图中) 获取数据的 Javascript:

//getDocs
    $.post('@Url.Action("GetDocs","Place", new { placeID = Model.PlaceId})').done(
        function (data) {
            console.log(data);
            var template = $.templates("#doc-tmpl");
            var htmlOut = template.render(data.documents);
            $("#documentListContainer").append(htmlOut);
            $(".docCount").html(data.count);
        });

(在视图中) 还有模板本身:

@* Document jsrender template *@
<script id="doc-tmpl" type="text/x-jsrender">
<div class="col-md-4">
    <a href="{{:docLink}}" target="_blank">
        <h5>{{:Name}}</h5>
        <p id="DocId" class="hidden"><br />{{:DocId}}</p>
        <img src="data:image;base64,{{:FrontCoverImg}}" width="140" height="230" />
    </a>
</div>
</script>

【讨论】:

    猜你喜欢
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 2020-12-24
    相关资源
    最近更新 更多