Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用jsrender标记在@Url.Action()中提供routevalue_C#_Json_Asp.net Mvc_Jsrender_Url.action - Fatal编程技术网

C# 使用jsrender标记在@Url.Action()中提供routevalue

C# 使用jsrender标记在@Url.Action()中提供routevalue,c#,json,asp.net-mvc,jsrender,url.action,C#,Json,Asp.net Mvc,Jsrender,Url.action,我正在从事一个使用jsrender的MVC项目。 这是我第一次使用jsrender(事实上,我对javascript和C#too lol还相当陌生),我已经为一个特定的问题挣扎了一整天 我有以下javascript $.post('@Url.Action("GetDocs","Place", new { placeID = Model.PlaceId})').done( function (data) { var template = $.templat

我正在从事一个使用jsrender的MVC项目。 这是我第一次使用jsrender(事实上,我对javascript和C#too lol还相当陌生),我已经为一个特定的问题挣扎了一整天

我有以下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()语句添加到这些键/值对中,例如:

  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
 }
"viewdoc" : '@@Url.Action("DisplayPDF", "Place", new {DocId = ' + DocId + ', @@class = "btn btn-default" })';
(不确定语法是否正确)这样我就可以将
{{{:viewdoc}}
放入模板中

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

试试这个

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

试试这个

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

资料来源:

好的,所以在一周的时间里,经过大量的实验和谷歌搜索,我找到了我的解决方案

我在控制器中创建了链接,其中设置了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);
@* 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>
(在视图中) 获取数据的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);
        });
(在视图中) 和模板本身:

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);
@* 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>
@*文档jsrender模板*@

好吧,在一周的时间里,我进行了大量的实验和谷歌搜索,终于找到了解决方案

我在控制器中创建了链接,其中设置了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);
@* 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>
(在视图中) 获取数据的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);
        });
(在视图中) 和模板本身:

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);
@* 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>
@*文档jsrender模板*@

我看到了问答,但不确定如何将其应用于我的情况。。也许我只是需要睡觉,哈哈。。如何将链接导入模板的html部分?我看到了该问答,但不确定如何将其应用于我的情况。。也许我只是需要睡觉,哈哈。。如何将链接导入模板的html部分?是否希望
?(我将
更改为
docId
更改为
docId
,因为您在上面的其他地方有
docId
。是的,可能应该是docId@borismore您想要
?(我更改了
docId
docId
,因为上面其他地方有
docId
。是的,可能应该是docId@borismore