简体   繁体   English

MVC3 Ajax.BeginForm没有返回特定的partialview

[英]MVC3 Ajax.BeginForm not returning specific partialview

I am having trouble returning a PartialView when calling it from an Ajax.BeginForm method. 从Ajax.BeginForm方法调用它时,我无法返回PartialView。 The Ajax call is as follows. Ajax调用如下。

@{
    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 在文本框中输入参数后,我单击Submit按钮,然后它转到ChargeCodeSearchController中的SearchChargeCode1 partialviewresult方法

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文件夹中的以下位置找到的_FindChargeCodeSearchResults1视图:

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

It does not! 它不是!

I put a breakpoint in the PartialViewMethod. 我在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". 它能够查询并查找结果列表并将其分配给chargecodes1变量,但是在它逐步执行返回代码行之后,它只会返回到包含Ajax的视图,但它不会插入局部视图更新目标“searchResults”中的“ChargeCodeSearch / _FindChargeCodeSearchResults1”。

I have a FirmSearch and MemberSearch folder under Shared that perform the exact same function, but with different objects. 我在Shared下有一个FirmSearch和MemberSearch文件夹,它执行完全相同的功能,但具有不同的对象。 Those work. 那些工作。 I don't know why this is not working. 我不知道为什么这不起作用。

Below is the code snippet of the view _FindChargeCodeSearchResult1 That should be returned. 下面是应该返回的视图_FindChargeCodeSearchResult1的代码片段。 When creating this cshtml view, I created it as a PartialView. 创建此cshtml视图时,我将其创建为PartialView。 I did not assign it a master page, nor did I strongly type it to the ChargeCode object. 我没有为它分配主页,也没有强烈地将它键入到ChargeCode对象中。

@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. 由Ajax.BeginForm调用的Action不会返回任何partail视图,因为它需要捕获PartailView RenderHtml,因为PartailView的返回类型是字符串。

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. RenderPartialViewToString方法返回部分视图的HtmlString。 and it will append the result in specify location of AjaxBeginForm "UpdateTargetId" attribute. 它会将结果附加到AjaxBeginForm“UpdateTargetId”属性的指定位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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