Home > Back-end >  (Newbie to APS.NET)MVC possibility for partilal page refresh without using Ajax
(Newbie to APS.NET)MVC possibility for partilal page refresh without using Ajax

Time:08-27

I have experience on Web development on Node JS(front and backend). Now I'm moving to latest Microsoft ASP.Net Core(.Net6) MVC. I love the way they are using c# code in Views(.cshtml). But I got into a situation that I could easily resolve by Javascript but not with c#. Below is the screen. I'm able to enter name and click on "Submit" button. It will return me the result grid. But I'm using return View code.

public IActionResult Index(Person p)
{
        string sPersonKeyword = HttpContext.Request.Form["Person_keyword"];
        DBDataGet(sPersonKeyword);
        return View();
}

The code above send back whole page, leading to my old value on search input box disappear. I know that I could change my technology to do MVC with Ajax in the cshtml, which I used to do in my Node JS projects. Is there any way I could do similar "Partial web page rendering" in cshtml views using c# coding. I tried to create and use Partial view, it still refresh the whole page.

return PartialView("_IndexPartial",recipes);

Below is how my screen looks like: enter image description here

When you click to vote, an HTTP request is issued, but the page does not jump, mainly using the HTTP204 status code.

204: It means that I have received it, but I have no data to return to you, please don't refresh the page. Don't jump to a new page.

Case 2: Use the iframe tag to achieve no page refresh.

html:

  //<div id = "box" >
  //< h1 > name </ h1 >
  //< p id="content">0</p>
  //<a href = "http://127.0.0.1:3000/1" target="tou">vote</a>
  //</div>
  //<iframe name = "tou" frameborder="0"></iframe>

nodejs:

      var http = require('http');
      http.createServer(function(req, res){ res.writeHead(200,{'Content-type':'text/html'}); res.end('ok');}).listen(3000);

enter image description here

In fact, there is a refresh in this method, but the content in the iframe frame is refreshed. Just set the width and height borders of the iframe to 0.

  

CodePudding user response:

@KamranShahid and @AbyssWatch818 Thanks guys for the answers, in the end I was able to do regular c# MVC for most of the logic. And only that partial grid upgrade with Ajax. I guess that is currently only way. Let's hope Microsoft develop c# style of Ajax soon. Because I struggle a lot during debugging that Ajax part. But c# allow you to debug directly on cshtml file, which was amazing.

  • Related