Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6032739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:22:42+00:00 2026-05-23T05:22:42+00:00

I’m new to MVC/Jquery and am having a problem. I have a form containing

  • 0

I’m new to MVC/Jquery and am having a problem. I have a form containing a WebGrid with edit/delete anchors on each row. When the user clicks edit I need to run some script to load a json object that the controller needs before returning an edit view. Using a submit button works fine but when I try to post via Ajax the edit view never appears, the form with the grid just reloads.

The form

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "shopform" }))
{
    <div id="grid" class="gridWrapper1">
    </div>
}

The script

        function EditShop(id) {
                idShop = id; // save id for json scraper later
                $("#shopform").submit();
                return 0;
        }

       $(document).ready(function() {
            $("#shopform").submit(function(){
                var jsonData = GetJsonModel(); // grabs various fields including idShop
                $.ajax({
                    type:'POST',
                    url:'/Admin/Shops/Edit/',
                    data: jsonData,
                    contentType: 'application/json; charset=uft-8',
                    error: ajaxError,
////// edit with solution here
                    success: function(data) {
                       $(document)[0].body.innerHTML = data;  // contains View returned by 
                    }                                         // controller action
                });
                return false; 
            });
        });

Controller code to build the grid column with edit/delete anchor tags:

    grid.Column(format: (item) =>
    {
      return new HtmlString(
           "<a href='JavaScript:void(0)' name='editShop' value='" +
              item.ShopID.ToString() +
              "' class='linkbutton' " + "onclick='EditShop(" + item.ShopID.ToString() + 
              ");'>Edit</a>&nbsp;&nbsp;&nbsp;" +
           "<a href='JavaScript:void(0)' name='delShop' value='" +
              item.ShopID.ToString() +
              "' class='linkbutton' onclick='DelShop(" +
              item.ShopID.ToString() + ");'>Remove</a>"
        );
}),

And finally the Controller actions for PRG for editing:

    [HttpPost]
[ActionName("Edit")]
[ViewModelFilter(Param = "model", JsonDataType = typeof(ShopsViewModel))]
public ActionResult EditViaPost(ShopsViewModel model)
{
    int nid = model.ShopID;
    ShopViewModel shop = new ShopViewModel(nid);
    Session["model"] = (object) model;
    return RedirectToAction("Edit", new RouteValueDictionary(new { shop.Name }));
}

[HttpGet]
[ActionName("Edit")]
public ActionResult EditViaGet(string slug) //, string curPage, string rowsPerPage )
{
    ShopsViewModel model = (ShopsViewModel)Session["model"];
    ShopViewModel shop = null;
    if (model != null)
    {
        int nID = model.ShopID;
        ShopViewModel shop = new ShopViewModel(nID);
    } ... // create empty shop omitted for brevity

    return View("Edit", shop);  <--- THIS DOES NOT LOAD IF SUBMITTING VIA AJAX
}

If I add the controller/action to the form declaration and submit with a button the edit view does render so I suspect I’m not handling the return from $.post correctly, but I need to return more than just the ID so that’s why I’m using JSON.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:22:42+00:00Added an answer on May 23, 2026 at 5:22 am

    Nothing is happening because you have no success callback defined on your Ajax call to handle the data returned by the controller. Your view will be returned in the success handler.

    $("#shopform").submit(function(){
        var jsonData = GetJsonModel(); // grabs various fields including idShop
        $.ajax({
            type:'POST',
            url:'/Admin/Shops/Edit/',
            data: jsonData,
            contentType: 'application/json; charset=uft-8',
            success: function(yourView) {
                alert(yourView);
            },
            error: ajaxError
        });
        return false; 
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.