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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:35:47+00:00 2026-06-12T05:35:47+00:00

I am having trouble returning a PartialView when calling it from an Ajax.BeginForm method.

  • 0

I am having trouble returning a PartialView when calling it from an Ajax.BeginForm method. The Ajax call is as follows.

@{
    ViewBag.Title = "ChargeCode 1";
    Layout = "~/Views/Shared/_Search_Layout.cshtml";
}

<div class="span9">
    <div class="page-header">
        <h1>Charge Code1</h1>
    </div>
        @using (Ajax.BeginForm("SearchChargeCode1", "ChargeCodeSearch", new AjaxOptions
        {
            UpdateTargetId = "searchResults",
            HttpMethod = "GET",
            InsertionMode = InsertionMode.Replace,
        }))
        {

            <input type="text" name="chargeCode1" class="input-medium search-query" />

            <input type="submit" class="btn" value="Search" />
            <button type="button" class="btn">Clear Results</button>

        }

    <table id="searchResults">

    </table>
</div>

After entering a parameter in the textbox, I click the Submit button and it then takes me to the SearchChargeCode1 partialviewresult method in the ChargeCodeSearchController

public PartialViewResult SearchChargeCode1(string chargeCode1)
{
var chargecodes1 = db.ChargeCodes.Where(c => (!(String.IsNullOrEmpty(chargeCode1)) && c.NameChargeCode.Contains(chargeCode1))).Take(10);

return PartialView("ChargeCodeSearch/_FindChargeCodeSearchResults1", chargecodes1);
}

It should then return the _FindChargeCodeSearchResults1 view that is found in the following location in the Views folder:

Views  
->Shared  
->->ChargeCodeSearch  
->->->_FindChargeCodeSearchResults1

It does not!

I put a breakpoint in the PartialViewMethod. It is able to query and find a list of results and assign it to the chargecodes1 variable, but after it steps through the return line of code, it just goes back to the view that contains the Ajax, but it does not insert the partial view “ChargeCodeSearch/_FindChargeCodeSearchResults1” in the update target “searchResults”.

I have a FirmSearch and MemberSearch folder under Shared that perform the exact same function, but with different objects. Those work. I don’t know why this is not working.

Below is the code snippet of the view _FindChargeCodeSearchResult1 That should be returned. When creating this cshtml view, I created it as a PartialView. I did not assign it a master page, nor did I strongly type it to the ChargeCode object.

@model IEnumerable<MLSMAS.Models.ChargeCode>
<table id="searchResults" class='table table-striped table-bordered table-condensed'>
    <thead>
        <tr>
            <th>ChargeCode ID</th>
            <th>ChargeCode Name</th>
            <th>ChargeCode Price</th>
            <th>Frequency</th>
            <th>ChargeCode Description</th>
            <th>Select</th>
        </tr>
    </thead>
    <tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.IdChargeCode)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.NameChargeCode)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Price)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Frequency)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DescChargeCode)
            </td>
            <td>
               <a href="" onclick="setChargeCode1('@item.IdMember' '@item.Price');" class="setChargeCode1">Select</a>
            </td>
         </tr>
    }   
    </tbody>
</table>
<script src="../../../Scripts/MLSMASJS.js" type="text/javascript"></script> 

Any help or suggestions is much appreciated.

  • 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-12T05:35:48+00:00Added an answer on June 12, 2026 at 5:35 am

    Action called by Ajax.BeginForm will not return any partail view it retrun string for this you need catch PartailView RenderHtml as return type of PartailView is string.

    In Controller

    public string SearchChargeCode1(string chargeCode1)
    {
    var chargecodes1 = db.ChargeCodes.Where(c => (!(String.IsNullOrEmpty(chargeCode1)) && c.NameChargeCode.Contains(chargeCode1))).Take(10);
    
    string RenderHtml = RenderPartialViewToString("ChargeCodeSearch/_FindChargeCodeSearchResults1", chargecodes1);
    
     return RenderHtml ;
    }
    
    
    protected string RenderPartialViewToString(string viewName, object model)
            {
                if (string.IsNullOrEmpty(viewName))
                    viewName = ControllerContext.RouteData.GetRequiredString("action");
    
                ViewData.Model = model;
    
                using (StringWriter sw = new StringWriter())
                {
                    ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                    ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                    viewResult.View.Render(viewContext, sw);
                    return sw.GetStringBuilder().ToString();
                }
            }
    

    RenderPartialViewToString method return HtmlString of partial view. and it will append the result in specify location of AjaxBeginForm “UpdateTargetId” attribute.

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

Sidebar

Related Questions

I'm having trouble returning arrays from a custom method. It compiles fine but I
I'm having trouble getting a response back from a Jquery ajax call... (It's a
I'm having trouble returning data from my controller using an ajax post function. What
I am having some trouble returning matrix data from one method to the main
I am having trouble returning data from my sql-server database to an aspx page
I am having trouble in returning compressed response (GZip) from my Java Servlet, to
I'm having trouble developing a standard for returning data from user functions passed and
I am having trouble returning only checked items from a list of items. Instead
I am having trouble in returning data in php from an SQL query. here
I am having trouble returning multiple entities (eager loading) using the Include() method. I

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.