MVC3 Ajax.BeginForm不返回特定的partialview(MVC3 Ajax.BeginForm not returning specific partialview)

编程入门 行业动态 更新时间:2024-10-24 21:24:01
MVC3 Ajax.BeginForm不返回特定的partialview(MVC3 Ajax.BeginForm not returning specific partialview)

从Ajax.BeginForm方法调用时,我无法返回PartialView。 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>

在文本框中输入参数后,我单击提交按钮,然后它将我带到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); }

应该返回在Views文件夹的以下位置找到的_FindChargeCodeSearchResults1视图:

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

它不是!

我在PartialViewMethod中放置了一个断点。 它能够查询和查找结果列表并将其分配给chargecodes1变量,但是在遍历代码的返回行后,它会返回到包含Ajax的视图,但不会插入部分视图更新目标“searchResults”中的“ChargeCodeSearch / _FindChargeCodeSearchResults1”。

我在Shared下有一个FirmSearch和MemberSearch文件夹,它执行完全相同的功能,但具有不同的对象。 那些工作。 我不知道为什么这不起作用。

下面是视图的代码片段_FindChargeCodeSearchResult1应该返回。 当创建这个cshtml视图时,我将它创建为PartialView。 我没有给它分配一个母版页,也没有强制将它输入到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>

任何帮助或建议非常感谢。

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.

最满意答案

由Ajax.BeginForm调用的动作不会返回任何partail视图它会为此重试字符串,因此您需要捕获PartailView RenderHtml,因为PartailView的返回类型是字符串。

在控制器中

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方法返回部分视图的HtmlString。 并将结果附加到指定AjaxBeginForm“UpdateTargetId”属性的位置。

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.

更多推荐

本文发布于:2023-07-28 01:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1298247.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:BeginForm   Ajax   specific   returning   partialview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!