I am having trouble returning a PartialView when calling it from the Ajax.BeginForm method. 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 the parameter in the text box, I click the Submit button and then returns me to the partialviewresult SearchChargeCode1 method in 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); }
Then it should return the _FindChargeCodeSearchResults1 view, which is located in the following location in the Views folder:
Views ->Shared ->->ChargeCodeSearch ->->->_FindChargeCodeSearchResults1
This is not true!
I set a breakpoint in PartialViewMethod. It can query and find the list of results and assign it to the variable chargecodes1, but after it runs the return code line, it just goes back to the view containing Ajax, but does not insert the partial view "ChargeCodeSearch / _FindChargeCodeSearchResults1" in the update target "searchResults" .
I have a FirmSearch and MemberSearch folder in the Shared section, which perform the same function, but with different objects. These work. I do not know why this does not work.
The following is a snippet of code like _FindChargeCodeSearchResult1. It must be returned. By creating this cshtml view, I created it as a PartialView. I did not assign it the main page, and I did not type it strictly for 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 are greatly appreciated.