Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
url.action call with parmeter in javascript

I want to load an image from the database when on page load and when clicking a button.

when form is loading it is working fine, but when i click the button, i need to pass the value from the dropdown and call the controller on javascript using Url.Action.

when i click the button, getting Failed to load resource: the server responded with a status of 500 ()

controller method:

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");
        }

View:

<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>

the error on the browser: enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

i would change ajax to this

$('#AdImage').attr('src','@Url.Action("MyAction", "AdImage")' + '?path=img/jpg&compId=' + cid);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!