在页面加载和单击按钮时,我希望从数据库中加载图像。
当表单加载时,它正常工作,但是当我单击按钮时,我需要从下拉列表中传递值,并使用Url.Action调用javascript上的控制器。
单击该按钮时,加载资源失败:服务器响应状态为500 ()
控制器方法:
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");
}
查看:
<body>
<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> Load Company Image</button>
</div>
<div>
<img src= '@Url.Action("MyAction", "AdImage", new{path="img/jpg", compId = ViewBag.compId})' id="AdImage"/>
</div>
</div>
</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>
浏览器上的错误:
发布于 2022-02-21 16:06:01
我会把ajax改为这个
$('#AdImage').attr('src','@Url.Action("MyAction", "AdImage")' + '?path=img/jpg&compId=' + cid);
https://stackoverflow.com/questions/71204025
复制相似问题