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 452463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:04:30+00:00 2026-05-12T22:04:30+00:00

I know this has been an issue for others, but I’ve yet to find

  • 0

I know this has been an issue for others, but I’ve yet to find anything that fixes my problem.

I have a partial view that is displayed in a lightbox (colorbox). It is a simple form. I want the form to submit and return a little bit of data. The data will be used in calling subsequent functions, and I want the main DIV just to be updated with a “success” message. Here is the full code of the partial view:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Solution2.Models.Category>" %>

<script type="text/javascript">
    $('propcatform').submit(function(e) {
        e.preventDefault();

        $.ajax({
            type: "POST",
            url:  $(this).attr("action"),
            data: $(this).serialize(),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function(data) { document.getElementById('main1').innerHTML = "Success"; },
            failure: function() { document.getElementById('main1').innerHTML = "Failure"; }
        })
    });
    </script>

    <% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "propcatform", name = "propcatform" }))
       {%>
        <div id="main1">
        <fieldset>
            <legend>Fields</legend>
            <p>
                <label for="CatTitle">Category Title:</label>
                <%= Html.TextBox("CatTitle") %>
                <%= Html.ValidationMessage("CatTitle", "*") %>
            </p>
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
        </div>

    <% } %>

Here is my controller code. The code works, in that it successfully adds the form data to the table/database. What exactly should my “return” line look like?

[AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Create(Category propcat)
        {

            Category resultcat = new Category();
            _db.Categories.InsertOnSubmit(propcat);
            _db.SubmitChanges();
            resultcat = propcat;
            return Json(new { CatID = resultcat.CatID, CatTitle = resultcat.CatTitle, message = "Category successfully created!" });
        }

Currently I’m not actually using any of the result data in my partial view code (even though I reference it in my “success” parameter). I’m just trying to get it to work (and not prompt me to save the results).

Thanks.

  • 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-12T22:04:30+00:00Added an answer on May 12, 2026 at 10:04 pm

    Typically you’re not going to return Json to the user’s browser directly through a normal form submittal. It’s a great way to pass information that you plan to digest internally (as you describe) but doing what you show above will spit back Json data to the user which just looks like…. Javscript — it’s not valid HTML and depending on how the browser is configured, it may pop-up a “Save As” dialogue.

    In your example, you could try to have the default action be to return the Create view and have a check like this:

    if (Request.IsAjaxRequest())
    {
      return Json(.......);
    }
    else
    {
      return View(....);
    }
    

    …and change your function return type to ActionResult.

    This will ensure that the initial load of the page displays the actual View. Ajax calls to the same page (from buttons on the page) will return just the Json data that you’re interested in.

    UPDATE:

    If the action should display the same page except for the updated Json content, I’d recommend not using a Submit button. Just use a normal input button and bind the click action to your jQuery.ajax call. The data will be received from the Create/POST action and will populate per the success: clause in the Ajax call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this issue has been touched on here but I have not found
I know this has been asked many times but i'm yet to find a
I know this has been discussed ad nauseum...but I have an issue with the
I know this has been asked thousands of times but I just can't find
I know this has been asked in the past, but I have seen no
I know this has been asked multiple times here, but I've a different issue
I know this topic has been discussed, but not by me yet. As I
I know this probably has been asked before but I am having issues with
I know this has been asked at least a thousand times but I can't
I know this has been asked different ways several times, but I'm just not

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.