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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:28:02+00:00 2026-06-04T13:28:02+00:00

I am using MVC4. Unobtrusive JavaScript is enabled and referenced in my project. The

  • 0

I am using MVC4. Unobtrusive JavaScript is enabled and referenced in my project. The Ajax scripts are loading in my shared master page. FireBug reports no errors or 404’s.

I have added a Text box to my page, which updates the contents of a Hidden field inside of my Ajax form. When a user presses a key, and the KeyUp event fires, I force my Ajax form to submit by calling:

$("form#ajaxForm").trigger("submit");

Where ajaxForm is the name of the Ajax form. This works fine. The form is submitted, behind the scenes, and my controller event fires. The Model is updated with the new data, based on the input from the user.

However, where it gets strange, is when the result returns to my form. The entire contents of my form div tag is replaced by the result — which is the entire page.

<div id="basic-modal-content"">
   <input id="breed" type="text"/>
<div>
<form id="ajaxForm" method="post" data-ajax-update="#results" data-ajax-mode="replace" data-ajax="true" action="/Poultry/ListBreeds?Length=7">
   <div id="results">
    ->THIS AREA IS GETTING REPLACED WITH THE ENTIRE FORM <-
      <div id="basic-modal-content">
      </div>
   </div>
</form>
  • Notice how basic-modal-content now contains itself, another basic-modal-content. The result div should only contain the updates results, not the entire view — obviously.

My code is as follows:

View (BreedSelectorPartial.cshtml)

@model IQueryable<Inert.BreedViewModel>

<script type="text/javascript">
    $(document).ready(function () {
        $("#breed").keyup(function (e) {
            $("input[name=breedSearch]").val($("#breed").val());
            $("form#ajaxForm").trigger("submit");
        });
    });
</script>

<div id="basic-modal-content">
<input id="breed" type="text" style="width:100%; height:22px;" />
    <div>
        <div style="margin-top: 4px;">
            <label for="breed">Begin by typing a breed, e.g. <span style="font-style: italic">Wyandotte</span></label>
        </div>
    </div>
    @using (Ajax.BeginForm("ListBreeds", "Poultry", new AjaxOptions {UpdateTargetId = "results" }, new { id = "ajaxForm" }))
    {
        <input id="breedSearch" name="breedSearch" type="hidden" />
    }
    <div id="results" style="height: 320px; color: black; margin-top: 12px; font-size: 18px;">
        @{
            var grid = new WebGrid(Model, ajaxUpdateContainerId: "results");
            int c = Model.Count();
         }
        @grid.GetHtml(tableStyle: "webgrid",
            alternatingRowStyle: "webgrid-alternating-row",
            rowStyle: "webgrid-row-style",
            displayHeader: true)
    </div>
</div>
<script type="text/javascript" src="/scripts/jquery.simplemodal.js"></script>
<script type="text/javascript" src="/scripts/basic.js"></script>

Controller (PoultryController.cs)

[HttpPost]
public ActionResult ListBreeds(string breedSearch)
{
    InertDatabaseEntitiesConnection db = new InertDatabaseEntitiesConnection();
    var breeds = db.Breeds.Where(q => q.Name.Contains(breedSearch));
    var names = breeds.Select(q => new BreedViewModel { Name = q.Name });
    return PartialView("BreedSelectorPartial", names);
}

I’m not sure where I’m going wrong on this. I’ve spent hours trying to figure this out. Any solutions?

  • 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-04T13:28:03+00:00Added an answer on June 4, 2026 at 1:28 pm

    The first issue is that you are using a wrong overload of the Ajax.BeginForm helper (but that’s not causing the undesired behavior, it’s just a sidenote):

    @using (Ajax.BeginForm(
        "ListBreeds",                                      // actionName
        "Poultry",                                         // routeValues
        new AjaxOptions { UpdateTargetId = "results" },    // ajaxOptions
        new { id = "ajaxForm" })                           // htmlAttributes
    )
    

    whereas you need:

    @using (Ajax.BeginForm(
        "ListBreeds",                                      // actionName
        "Poultry",                                         // controllerName
        null,                                              // routeValues
        new AjaxOptions { UpdateTargetId = "results" },    // ajaxOptions
        new { id = "ajaxForm" })                           // htmlAttributes
    )
    

    Now the real issue is that you need to put the #results div outside the partial and inside your main view:

    <div id="results" style="height: 320px; color: black; margin-top: 12px; font-size: 18px;">
        @Html.Partial("BreedSelectorPartial")
    </div>
    

    Now of course if you only need to update the WebGrid portion then it would make more sense to leave only the grid inside the partial and move the form outside in the main view.

    So your main view now becomes:

    @model IQueryable<BreedViewModel>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#breed").keyup(function (e) {
                $("input[name=breedSearch]").val($("#breed").val());
                $("form#ajaxForm").trigger("submit");
            });
        });
    </script>
    
    <div id="basic-modal-content">
        <input id="breed" type="text" style="width:100%; height:22px;" />
        <div>
            <div style="margin-top: 4px;">
                <label for="breed">Begin by typing a breed, e.g. <span style="font-style: italic">Wyandotte</span></label>
            </div>
        </div>
        @using (Ajax.BeginForm("ListBreeds", "Poultry", null, new AjaxOptions { UpdateTargetId = "results" }, new { id = "ajaxForm" }))
        {
            <input id="breedSearch" name="breedSearch" type="hidden" />
        }
        <div id="results" style="height: 320px; color: black; margin-top: 12px; font-size: 18px;">
            @Html.Partial("BreedSelectorPartial")
        </div>
    </div>
    

    and the only portion that needs to be updated is the BreedSelectorPartial.cshtml partial which contains the grid:

    @model IQueryable<BreedViewModel>
    @{
        var grid = new WebGrid(Model, ajaxUpdateContainerId: "results");
    }
    @grid.GetHtml(
        tableStyle: "webgrid",
        alternatingRowStyle: "webgrid-alternating-row",
        rowStyle: "webgrid-row-style",
        displayHeader: true
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the new mvc4 ajax controller template and I don't know how
I'm using ASP.net MVC4 with jquery mobile. i'm trying to change the transition page
I am starting a new project using MVC4 and would like to use dependency
EDIT - We're using MVC4 Dev Preview.... I'm implementing an edit page for a
I am using MVC4's WEB API to expose a controller. Initially I created created
I have been writing an application using ASP.Net MVC4, where the majority of the
I'm using EF 4.3 in an MVC4 application and Ninject.MVC3. Controllers are MVCscaffolded using
I've created a method using the new WebAPI features in MVC4 and have it
I'm using the single page application template in MVC 4. The template created the
I'm trying to convert a form from synchronous to asynchronous using the Ajax.BeginForm helper

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.