Skip to content

Reload Index

KS LIM edited this page Feb 26, 2022 · 2 revisions
  • site.js
jQuery("#btn_searchcode").click(function () {
        window.location.href = _url + "Home/Index?" +
            "search_code=" + document.getElementById("search_code").value;
    });
  • HomeController.cs
public async Task<IActionResult> Index(string? search_code)
{
            Cost list = new Cost();
            List<Cost> cost_ = new List<Cost>();

            HttpClient clientdata = _api.Initial();

            var action = "api/data/get-all-costs";
            if (search_code!=null)
                action = "api/data/get-cost-by-search/" + search_code;

            HttpResponseMessage resdata = await clientdata.GetAsync(action);
            resdata.EnsureSuccessStatusCode();

            if (resdata.IsSuccessStatusCode)
            {
                var resultdata = resdata.Content.ReadAsStringAsync().Result;
                cost_ = JsonConvert.DeserializeObject<List<Cost>>(resultdata);
                foreach (var o in cost_)
                {
                    list.data.Add(new Cost
                    {
                        doc_no = o.doc_no,
                        wr_no = o.wr_no,
                        CostId = o.CostId
                    });
                }
            }
            List<Cost> model = list.data.ToList();
            ViewData["data"] = model;

            return View();
}
  • Index.cshtml
foreach (var i in (List<Cost>)ViewData["data"])
{
   <tr>
      <td>@i.doc_no</td>
      <td>@i.wr_no</td>
      <td style="display:none">@i.CostId</td>
      <td>
          <a style="height:25px;width:25px;cursor:pointer"  
             onclick="delete_data(@i.CostId)"
             class="btn btn-danger bg-light">
          <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" style="margin-left:-10px;margin-top:-15px"
               fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16">
               <path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z" />
               <path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z" />
         </svg>
        </a>
     </td>
   </tr>
}
Clone this wiki locally