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

  • Home
  • SEARCH
  • 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 7877615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:25:28+00:00 2026-06-03T03:25:28+00:00

i’m trying to post a form to my controller via ajax so i can

  • 0

i’m trying to post a form to my controller via ajax so i can render a partial view.

Here is my Ajax Code

 var formCollection = $('#some-form');
 $(function(){ $('#some-form').submit(function(){
      $.ajax({
           type: "POST",
           url: "/Trusk/Index",
           data: formCollection,
           dataType: "html",
           success: function (result) {
               $('#newView').html(result);
           },
           error: function (request, status, error) {
               alert('Oh no!');
           }
     });
});
}); 

Code for my form, i want partial view to be render at ID = newView, The partial view is returned by the controller

  <% using (Html.BeginForm(new { @id = "some-form" }))
           { %>
            <div id="TestDiv">
            <div id="Title">Test</div>                 
                <div id="CheckIn">Check-in:<br />
                <%:Html.TextBox("FromDate", "", new { @id = "DateFrom", @style =  "width:190px" })%></div>
                <div id="CheckOut">Check-out:<br />
                <%:Html.TextBox("ToDate", "", new { @id = "DateTo", @style =  "width:190px" })%><br /></div>
                <div id="newView">  
                </div>
                <input type="submit" value="Search" />        
        </div>
               </div>
        <% } %>

My controller code

 public ActionResult Index(FormCollection post)
    {
        ITruckRepository hotelRep = new TruckRepository();

        IEnumerable<Truck> AvailableTrucks = hotelRep.getTrucks('2012,3,2', '2012,3,5');

       return PartialView("Search", AvailableTrucks );

    }

Do i pass the correct parameter to the controller via Ajax?

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-06-03T03:25:29+00:00Added an answer on June 3, 2026 at 3:25 am

    There are a couple of issues with your code:

    • You are using a wrong overload of the Html.BeginForm method. In the overload you used the id is a route value, not HTML attribute
    • Inside your .submit callback you are not canceling the default action by returning false and thus when the form is submitted the AJAX call will hardly have any time to execute before the browser redirects away from the page
    • In the data parameter of the AJAX call you are passing a jQuery object representing the form called formCollection when you should be serializing the data.
    • You have broken markup => there’s one closing div that doesn’t have a corresponding opening element.

    So let’s start by fixing the markup first:

    <% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "some-form" })) { %>
        <div id="TestDiv">
            <div id="Title">Test</div>                 
            <div id="CheckIn">
                Check-in:<br />
                <%= Html.TextBox("FromDate", "", new { id = "DateFrom", style = "width:190px" }) %>
            </div>
            <div id="CheckOut">
                Check-out:<br />
                <%= Html.TextBox("ToDate", "", new { id = "DateTo", style = "width:190px" }) %>
                <br />
            </div>
            <div id="newView"></div>
            <input type="submit" value="Search" />        
        </div>
    <% } %>
    

    and then the script that will AJAXify the form:

    $(function () {
        $('#some-form').submit(function () {
            $.ajax({
                type: this.method,
                url: this.action,
                data: $(this).serialize(),
                success: function (result) {
                    $('#newView').html(result);
                },
                error: function (request, status, error) {
                    alert('Oh no!');
                }
            });
            return false;
        });
    }); 
    

    Now you should ensure that the corresponding POST controller action is successfully invoked and the parameters passed correctly. Also ensure that no errors occur inside this controller action and while rendering the partial view. Use a javascript debugging tool such as FireBug to see exactly what request/response are being sent as well as any possible js errors.

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

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
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 this code to decode numeric html entities to the UTF8 equivalent character.

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.