0

I am not that much efficient in mvc. I want to continuously try pinging the IPs in the list and then render the data in Index view using Viewbag. but problem here is I have to refresh the page manually each and every time to get the current status of each and every IP.

 public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {

            List<string> IPlist = new List<string>();
            IPlist.Add("10.0.1.151");
            IPlist.Add("www.google.com");
            IPlist.Add("192.168.0.1");
            Ping myping = new Ping();
            StringBuilder sc = new StringBuilder("Ping Status:");
            foreach (string c in IPlist)
            {
                PingReply replytest = myping.Send(c, 1000);

                if (replytest != null)
                {
                    sc.Append(" Status : " + replytest.Status + " \n Time: " + replytest.RoundtripTime.ToString() + " \n Address : " + replytest.Address + " \n ");
                }
                else
                {
                    sc.Append("Request Failed");
                }
            }

            ViewBag.result = sc.ToString();

            return View();
        }
    }
James Z
  • 12,209
  • 10
  • 24
  • 44
M. S
  • 5
  • 6
  • You could use a`BackgroundWorker`. https://stackoverflow.com/questions/6481304/how-to-use-a-backgroundworker – Paul Baxter Jul 28 '20 at 05:42
  • You can create new `action` which would return only your desired `string`. Call this `action` with `ajax` on some `interval` and update your DOM with `javascript`. – Karan Jul 28 '20 at 06:04
  • @karan can you please elaborate a little please – M. S Jul 28 '20 at 07:49

1 Answers1

0

Add new action which return a JsonResult from a controller to the ajax query. Simply convert the string to JsonResult by using Json(stringvalue);

public class HomeController : Controller
{
    public JsonResult GetCurrentStatus()
    {
        List<string> IPlist = new List<string>();
        IPlist.Add("10.0.1.151");
        IPlist.Add("www.google.com");
        IPlist.Add("192.168.0.1");
        Ping myping = new Ping();
        StringBuilder sc = new StringBuilder("Ping Status:");
        foreach (string c in IPlist)
        {
            PingReply replytest = myping.Send(c, 1000);

            if (replytest != null)
            {
                sc.Append(" Status : " + replytest.Status + " \n Time: " + replytest.RoundtripTime.ToString() + " \n Address : " + replytest.Address + " \n ");
            }
            else
            {
                sc.Append("Request Failed");
            }
        }
        
        return Json(sc.ToString());
    }
}

Add javascript function which call new action. And call this function every 5000 milliseconds with setInterval. On success of ajax I am setting retrieved value to element with id divPintStatus. You can change it as per your actual code. Also set @Url.Action as your own code.

setInterval(getCurrentStatus, 5000);

function getCurrentStatus() {
    $.ajax({
         url: '@Url.Action("GetCurrentStatus")',    // Or '@Url.Action("GetCurrentStatus", "ControllerName")',
         type: "GET",
         dataType: "json",
         processData: false,
         contentType: false,
         success: function (data) {
            $('#divPintStatus').val(data);
        }
    });
}
Karan
  • 12,059
  • 3
  • 24
  • 40