MVC3 Ajax.BeginForm does not return a specific partial view

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.

+4
source share
1 answer

The action called by Ajax.BeginForm will not return any partail view of its extraction string for this, you need to catch the PartailView RenderHtml, since the returned PartailView type is a string.

In the 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(); } } 

The RenderPartialViewToString method returns an HtmlString of a partial view. and it will add the result to the specified location of the AjaxBeginForm attribute "UpdateTargetId".

+1
source

Source: https://habr.com/ru/post/1433930/


All Articles