首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >javascript中带参数的url.action调用

javascript中带参数的url.action调用
EN

Stack Overflow用户
提问于 2022-02-21 09:36:40
回答 1查看 193关注 0票数 0

在页面加载和单击按钮时,我希望从数据库中加载图像。

当表单加载时,它正常工作,但是当我单击按钮时,我需要从下拉列表中传递值,并使用Url.Action调用javascript上的控制器。

单击该按钮时,加载资源失败:服务器响应状态为500 ()

控制器方法:

代码语言:javascript
运行
复制
public FileContentResult MyAction(int compId)
        {
            byte[] img = new byte[100000];
           
                img = _unitOfWork.Company.GetAll().Where(a => a.Id == compId).
                    Select(a => a.PhotoAd).FirstOrDefault();
            if (img == null)
            {
                return null;
            }
            return new FileContentResult(img, "img/jpg");
        }

查看:

代码语言:javascript
运行
复制
<body>
    &nbsp;
   <div class="col-4" >
       <div class="form-group row">
            <div class="col-2">
                <label asp-for="forCompList.CompId">Company</label>
             </div>
             <div class="col-8">
                   @Html.DropDownListFor(m => m.forCompList.CompId, Model.CompList,
                                                new { @class = "form-control" })
            </div>
        </div>
        <div class="col-2 text-right">
            <button class="btn btn-primary" style="background-color:forestgreen" onclick="LoadImage()">
                <i class="fa fa-refresh"></i> &nbsp; Load Company Image</button>
        </div>
        <div>
        <img src= '@Url.Action("MyAction", "AdImage", new{path="img/jpg", compId = ViewBag.compId})' id="AdImage"/>
        </div>
    </div>
    &nbsp;
   
</body>
@section Scripts{
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script type="text/javascript">
        function LoadImage() 
        {
            var cid = document.getElementById('forCompList_CompId').value;
            window.alert(cid);
            $('#AdImage').src = 
            '@Url.Action("MyAction", "AdImage",new{path="img/jpg"})?compId='+cid;
            
        }
    </script>

浏览器上的错误:

EN

回答 1

Stack Overflow用户

发布于 2022-02-21 16:06:01

我会把ajax改为这个

代码语言:javascript
运行
复制
$('#AdImage').attr('src','@Url.Action("MyAction", "AdImage")' + '?path=img/jpg&compId=' + cid);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71204025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档