You must Sign In to post a response.
  • Category: ASP.Net MVC

    How to prevent page refresh in MVC 5 application

    Hi Friends,

    I am using MVC 5, when ever i call any controller, my page is getting refreshed, how do i prevent page refresh.

    Based on my analysis, People are sysing I need to use Jquery-Ajax calling, could you please any one provide me code for Insert, delete, update using Jquery-Ajax.
  • #761972
    you can try to use the ajax updation using like

    Ajax.BeginForm()

    By Nathan
    Direction is important than speed

  • #761974
    i am using jquery ajax calling, below is my code.

    $(document).ready(function () {

    $('#btnCreateCategory').click(function () {
    $.ajax({
    type: "POST",
    url: "@Url.Action("Create", "Category")",
    traditional: true,
    data: { CategoryName: $('#CategoryName').val().trim() },
    success: successFunc,
    error: errorFunc
    });
    })

    function successFunc(data, status) {
    }

    function errorFunc() {
    alert('error')
    }

    });

    ---------------- Activity

    [HttpPost]
    public JsonResult Create(string CategoryName)
    {
    try
    {
    Tbl_Addnewcategory obj = new Tbl_Addnewcategory();
    obj.CategoryName = CategoryName;
    obj.Status = true;
    objBs.CategoryBs.Insert(obj);
    TempData["Msg"] = "Created Successfully";
    return Json("Inserted Successfully", JsonRequestBehavior.AllowGet);
    }
    catch
    {
    return Json("Some problem occur...try again", JsonRequestBehavior.AllowGet);
    }
    }


    still my page is refreshing.

    CategoryController.cs.txt

    Delete Attachment

  • #761977
    Can you try to give the url like as follows

    $(document).ready(function () {
    var ActURL = "/Category/Create/";
    $('#btnCreateCategory').click(function () {
    $.ajax({
    type: "POST",
    url: ActURL ,
    traditional: true,
    data: { CategoryName: $('#CategoryName').val().trim() },
    success: successFunc,
    error: errorFunc
    });
    })

    By Nathan
    Direction is important than speed

  • #761978
    above changes not working.

    https://www.dropbox.com/s/fs2j6wv5lcr0fyh/Inventorymanagemnt.rar?dl=0

    i have updated my code and database in above dropbox link, kindly have a look.

    please check only category page, i want to run any action with out refresh the page

  • #761979
    Hi
    You can go through below links

    http://www.codeexplain.com/code/c0a5ebbc-cd63-42f6-821a-060fc2f55c80

    http://iswwwup.com/t/5992ba3661cc/html-mvc-5-prevent-page-refresh-on-form-submit.html

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #761980
    above changes not working.

    https://www.dropbox.com/s/fs2j6wv5lcr0fyh/Inventorymanagemnt.rar?dl=0

    i have updated my code and database in above dropbox link, kindly have a look.

    please check only category page, i want to run any action with out refresh the page

  • #761988
    Hello Ashok,

    Refer the below links :

    http://www.codeproject.com/Articles/875859/Insert-Update-and-Delete-MVC-WebGrid-Data-using-JQ

    http://kushonline.blogspot.in/2015/02/insert-delete-update-retrieve-data-with.html

    http://www.c-sharpcorner.com/Blogs/14611/insert-delete-update-read-crud-operation-in-mvc-4-0-using.aspx

    http://www.c-sharpcorner.com/UploadFile/cd7c2e/implement-insert-update-and-delete-functionality/

    http://www.webcodeexpert.com/2015/01/createreadupdatedelete-operation-using.html#.VeQ9tiWqpBc


    Hope this will work for you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #761997
    Hi

    You have to stop the default behaviour to avoid the page refresh.

    $(document).ready(function () {
    $('#btnCreateCategory').click(function () {
    event.preventDefault();
    });
    $.ajax({
    type: "POST",
    url: "@Url.Action("Create", "Category")",
    traditional: true,

    For insert,update and delete refer the below links

    http://www.codeproject.com/Articles/875859/
    Insert-Update-and-Delete-MVC-WebGrid-Data-
    using-JQ

    Regards

    Sridhar Thota.
    If you learn't from defeat..
    You haven't really lost..

    Sridhar Thota.
    Editor: DNS Forum.


Sign In to post your comments