Kingkong Kingkong - 16 days ago 5
C# Question

Change view content without refreshing page or creating a new view

I'm working on a multiplayer quiz, and I'm currently in a situation where i have to change the contents of my quiz view where I have 1 question and 4 answers, but everytime the user clicks on an answer (which is a button), I need to populate the buttons (answers) and the question with new values.

What would be a simple way of achieving this page refresh without actually having to carry an ID as parameter in my url everytime?

Answer

e.g. you can use ajax

c#

public class CustomerController : Controller 
{
   public ActionResult Index() 
   {
       return View();
   }

   [HttpPost]
   public ActionResult GetUpdate()
   {
      return Json(new { success = true, newPar = 4 }, JsonRequestBehavior.AllowGet);
   }
}

Ajax:

$.ajax({
        url: '@Url.Action("GetUpdate")',
        dataType: "json", 
        type: "POST", 
        contentType: 'application/json; charset=utf-8', 
        cache: false, 
        data: {}, //pass argument if needed
        success: function (data) {
            if (data.success) { 
                alert(data.newPar);
            }
        },
        error: function (xhr) {
            alert('ERROR');
        }
    });

another option is to use SignalR:

the libary does all for you and you can call methods from serverside https://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr-and-mvc