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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:24:48+00:00 2026-06-11T09:24:48+00:00

i have four different forms on my page and each are ajax forms. I’m

  • 0

i have four different forms on my page and each are ajax forms.

I’m sending post request for first form with ajax to MVC Controller, it basically returns ViewData[“TEST”] back to me.

I want to use ViewData on my view and i need set this to a hidden field for use other forms.

How i can reach it without using normal submit ?

Here is my code:

@using (Ajax.BeginForm("Index", new AjaxOptions{ HttpMethod = "POST" }))
{
    <script type="text/javascript"> alert('@(ViewData["TEST"])'); </script>
    <input type="text" name="name" />
    <input type="button" onclick="javacript:SubmitAjax();" />
}

<script type="text/javascript">
    function SubmitAjax() {
        $.ajax({
            type: 'POST',
            data: $("#form0").serialize(),
            url: "/Home/Index",
            timeout: 2000,
            async: false,
            success: function (data) {
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(message_Error);
            }
        });
    }

And Controller;

    [HttpPost]
    public ActionResult Index(string name)
    {
        ViewData["TEST"] = "TESTSTRING";
        return View();
    }
  • 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-06-11T09:24:49+00:00Added an answer on June 11, 2026 at 9:24 am

    No ViewData !!!! . Simply return the content.

    [HttpPost]
    public ActionResult Index(string name)
    {
       return Content("TESTSTRING");
    }
    

    and to set this in the hidden field,you can do so int he success event of your ajax function

    success: function (data) {
          $("#hiddenElementID").val(data);
    },
    

    Also do not hard code the Path to action method like that. Always make use of the HTML helper methods.

    Replace

    url: "/Home/Index"
    

    with

    url: "@Url.Action("Index","Home")"
    

    I personally prefer to avoid the AjaxBeginForm method and would like to write some clean handwritten javascript code to handle this.

    @using(Html.Beginform())
    {
      <input type="text" name="name" />
      <input type="submit" id="saveName" value="Save" />
    } 
    
    <script type="text/javascript">
     $(function(){
      $("#saveName").click(function(e){
         e.preventDefault();
          $.post("@Url.Action("Index","Home")", 
                            $(this).closest("form").serialize(),
                                                                function(data){
              $("#yourHiddenElementID").val(data);
          });     
      });    
     });    
    </script>
    

    EDIT : As per the comment.

    If you want to return multiple items, You can return JSON

    Ex 2 : returning anonymous type to JSON

    [HttpPost]
    public ActionResult Index(string name)
    {
       return JSON(new { status : "true", ItemCount=35, UserName="Marc"} );
    }
    

    Ex 1 : returning a ViewModel to JSON

    Assuming you have a class like

    public class Result
    {
      public string Status { set;get;}
      public int ItemCount { set;get;}
      public string UserName  { set;get;}
    }
    

    Now you can use this class and return it as JSON

    [HttpPost]
    public ActionResult Index(string name)
    {
       return JSON(new Result { Status : "true",
                                ItemCount=25, UserName="Scott"} );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an html form (each form has different page, according to date), with
Hi I have a page for which I have four different style sheets like
Currently I have Long Pres Gesture Recognizers on four different TableViews (two in each
I have a page that needs to combine data from four different webrequests into
I have a form and up the top it contains four different radio buttons
I have four different Business objects and each calls its corresponding FillBusinessObject method to
I have a form with four different fieldsets. For jQuery validation, I would like
I have a form with four dropdowns, each with X amount of values listed;
I have an asp:Menu control that has four different menu items in it. Each
I have a form that handles four different types of facets of the same

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.