Search code examples
c#asp.net-mvcviewbag

how to fetch the data from adminside and then display in client side?


currently 3 vacancy available in admin side and I want to display them in client side carrer page if admin side delete the all vacancy then display the client side no vacancy available now

admin side (use entity framework):

        [HttpGet]
        [AllowAnonymous]
        public ActionResult DisplayVacancyData()
        {
            var list = _dbfltEntities.tbl_vacancy.ToList();
            return View(list);
        }

see admin side view:

enter image description here

client side (not use entity framework)

HomeController.cs

        [HttpGet]
        public async Task<ActionResult> Career(Vacancy _vacancy)
        {
            HttpClient client = new HttpClient();
            HttpResponseMessage responseMessage = client.GetAsync("https://localhost:44325/Home/DisplayVacancyData").Result;

            if (responseMessage.IsSuccessStatusCode)
            {
                await responseMessage.Content.ReadAsStringAsync();
            }
            return View();
        }

see response is ok but which logic should be apply to display the data

enter image description here

vacancy.cs

    public class Vacancy
    {
        public int vacancyid { get; set; }
        public string vacancytitle { get; set; }
        public string vacancyposition { get; set; }
        public string vacancyexperience { get; set; }
        public string vacancyjobdescription { get; set; }
        public string vacancyrequiredskil { get; set; }
    }

careerpage.cshtml

<style type="text/css">
    .actionlink {
        font-size: 25px;
        color: cornflowerblue;
    }
</style>

<div class="container">
    <div class="row">
        <div class="col-md-6">
            <h2 style="margin-top:150px;">Current Job Openings</h2>
        </div>
        <div class="col-md-4" style="margin-top:155px;">
            @Html.ActionLink("Apply for Job", "ApplyForJob", null, new { @class = "actionlink" })
        </div>
    </div>
    <div class="row">
        <p> ssss ss sss a sss s of an ss-sssss s ss? s ss with s, ssss s enthusiassm s s? s s aside s s s s s s. s over ssss s s s s s sss below:</p>
    </div>
</div>

I am trying to get the data using viewbag

HomeController.cs

            if (responseMessage.IsSuccessStatusCode)
            {
                await responseMessage.Content.ReadAsStringAsync();

                ViewBag.vacancytitle = _vacancy.vacancytitle;  //I am seeing in watch window but it is null

            }

accessing a view bag in career view page

@ViewBag.vacancytitle

help


Solution

  • Return value of a function responseMessage.Content.ReadAsStringAsync() is ignored.

    Admin side (use entity framework), add action (return json):

        [HttpGet]
        [AllowAnonymous]
        public ActionResult GetVacancyData()
        {
            var list = _dbfltEntities.tbl_vacancy.ToList();
            return new JsonResult(list);
        }
    

    HomeController.cs
    (client URI "https://.../Home/GetVacancyData" )

       [HttpGet]
        public async Task<ActionResult> Career()
        {
            HttpClient client = new HttpClient();
            HttpResponseMessage responseMessage = client.GetAsync("https://localhost:44325/Home/GetVacancyData").Result;
    
              if (responseMessage.IsSuccessStatusCode)
            {
                string result = await responseMessage.Content.ReadAsStringAsync();
                List<Vacancy> products = JsonConvert.DeserializeObject<List<Vacancy>>(result);
                return View(products);
            }
    
            return View();
        }
    

    careerpage.cshtml

      @foreach (var item in Model)
        {
            <p>@item.vacancytitle</p>
        }