简体   繁体   中英

MVC3 Ajax.BeginForm not returning specific partialview

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM