.NET MVC Ajax 表单 - 如何隐藏它?

发布于 2024-08-27 00:51:38 字数 1811 浏览 8 评论 0原文

好吧,一切都“功能性”地与我试图完成的任务一起工作,我再次确信这是愚蠢的事情,但我不知道如何做这一件事。

我有一个实体的编辑表单,比如说一辆汽车。这辆“汽车”可以有 0 至多名乘客。因此,在我的编辑表单上,我有汽车的所有字段,然后是显示每位乘客(部分)的列表视图。我还有一个“添加新乘客”按钮,它将呈现一个新的部分视图,允许您输入乘客。它有一个取消链接和一个用于提交 Ajax 表单的添加按钮。当您添加乘客时,乘客会自动添加到列表中,但我需要输入乘客表格才能消失。我尝试使用 onSuccess 和 onComplete 函数来隐藏表单所在的 div,但两者都只渲染部分视图 HTML 元素(白屏、文本),而不渲染整个页面上下文中的partialView。

资料来源: 1) 主编辑视图

    <script type="text/javascript">
    Function hideForm(){
        document.getElementById('newPassenger').style.display = 'none';
    }

</script>
<h2>Edit</h2>

<%-- The following line works around an ASP.NET compiler warning --%>
<%= ""%>

<%Html.RenderPartial("EditCar", Model)%>
<h2>Passengers for this car</h2>
<%=Ajax.ActionLink("Add New Passenger", "AddPassenger", New With {.ID = Model.carID}, New AjaxOptions With {.UpdateTargetId = "newPassenger", .InsertionMode = InsertionMode.Replace})%>
<div id="newPassenger"></div>
<div id="passengerList">
    <%Html.RenderPartial("passengerList", Model.Passengers)%>
</div>
<div>
    <%= Html.ActionLink("Back to List", "Index") %>
</div>

2) 添加乘客视图。下面的取消链接是一个不返回任何内容的操作,从而删除了 div 中的信息。

<%  Using Ajax.BeginForm("AddPassengerToCar", New With {.id = ViewData("carID")}, New AjaxOptions With {.OnSuccess = "hideForm()", .UpdateTargetId = "passengerList", .InsertionMode = InsertionMode.Replace})%>   
    <%=Html.DropDownList("Passengers")%> 
    <input type="submit" value="Add" />
    <%=Ajax.ActionLink("Cancel", "CancelAddControl", _
                                            New AjaxOptions With {.UpdateTargetId = "newPassenger", .InsertionMode = InsertionMode.Replace})%><% end using %>

Ok, everything is 'functionally' working with what I am attempting to accomplish and once again, I am sure this is something dumb, but I cannot figure out how to do this one thing.

I have an edit form for an entity, lets say a car. This 'car' can have 0 - many passengers. So on my edit form, I have all the fields for the car, then a list view showing each passenger (partial). I also have a 'add new passenger' button that will render a new partial view that allows you to enter a passenger. This has a cancel link and an add button to submit an Ajax form. When you add a passenger, the passenger is automatically added to the list, but I need the enter passenger form to go away. I have tried using the onSuccess and onComplete functions to hide the div that the form is in, but both render just the partial view HTML elements (white screen, text) and not the partialView in the context of the entire page.

Sources:
1) Main Edit View

    <script type="text/javascript">
    Function hideForm(){
        document.getElementById('newPassenger').style.display = 'none';
    }

</script>
<h2>Edit</h2>

<%-- The following line works around an ASP.NET compiler warning --%>
<%= ""%>

<%Html.RenderPartial("EditCar", Model)%>
<h2>Passengers for this car</h2>
<%=Ajax.ActionLink("Add New Passenger", "AddPassenger", New With {.ID = Model.carID}, New AjaxOptions With {.UpdateTargetId = "newPassenger", .InsertionMode = InsertionMode.Replace})%>
<div id="newPassenger"></div>
<div id="passengerList">
    <%Html.RenderPartial("passengerList", Model.Passengers)%>
</div>
<div>
    <%= Html.ActionLink("Back to List", "Index") %>
</div>

2) AddPassenger View. The cancel link below is an action that returns nothing, thus removing the information in the div.

<%  Using Ajax.BeginForm("AddPassengerToCar", New With {.id = ViewData("carID")}, New AjaxOptions With {.OnSuccess = "hideForm()", .UpdateTargetId = "passengerList", .InsertionMode = InsertionMode.Replace})%>   
    <%=Html.DropDownList("Passengers")%> 
    <input type="submit" value="Add" />
    <%=Ajax.ActionLink("Cancel", "CancelAddControl", _
                                            New AjaxOptions With {.UpdateTargetId = "newPassenger", .InsertionMode = InsertionMode.Replace})%><% end using %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不爱素颜 2024-09-03 00:51:38

好吧 - 想通了,我想昨晚已经太晚了。所以,这就是答案,它实际上是一些小问题最终汇聚成的。

  1. javascript 中的函数 hideForm() 应该是 function hideForm(),没有大写。我想太习惯于编写控制器函数了。
  2. 我在 OnSuccess 属性中使用“hideForm()”,而不是“hideForm” - doh。
  3. 您可能不想隐藏该 div,因为当您再次单击“添加新乘客”链接时,该 div 将不会显示。更改了 hideForm 函数以将 div 的innerHTML 属性设置为“”。

再次感谢达林的帮助,我试图从中获得比结果更多的东西。

Ok - figured this out, I suppose it was getting too late last night. So, here is the answer, it actually turned out to be a few small issues culminating into one.

  1. Function hideForm() in the javascript should be function hideForm(), no capitals. Too used to writing controller functions I suppose.
  2. I was using "hideForm()" in the OnSuccess attribute, not "hideForm" - doh.
  3. You probably don't want to hide the div as when you click the add new passenger link again, the div won't display. Changed hideForm function to set the innerHTML property of the div to ''.

Thanks again darin for your assistance, I was trying to making more out of it than it turned out to be.

稀香 2024-09-03 00:51:38

确保您已在页面中引用了 MicrosoftAjax.jsMicrosoftMvcAjax.js 脚本。获取部分视图内容(白屏、文本)的原因可能是脚本错误,导致 ajax 无法正确执行。使用FireBug查看执行过程中是否出现错误。

至于隐藏表单,您可以使用 OnSuccess 回调:

<% Using Ajax.BeginForm("AddPassengerToCar", 
    New With { .id = ViewData("carID")}, 
    New AjaxOptions With {
        .UpdateTargetId = "passengerList", 
        .InsertionMode = InsertionMode.Replace,
        .OnSuccess = "hideForm"
}) %>

如果 AJAX 调用成功,将执行 hideForm javascript 函数,并隐藏所需的 div。

Make sure that you've referenced both the MicrosoftAjax.js and MicrosoftMvcAjax.js scripts in your page. The reason for getting the partial view contents (white screen, text) could be a script error which prevents the ajax from executing correctly. Look with FireBug if there are some errors during the execution.

As far as hiding the form is concerned you could use the OnSuccess callback:

<% Using Ajax.BeginForm("AddPassengerToCar", 
    New With { .id = ViewData("carID")}, 
    New AjaxOptions With {
        .UpdateTargetId = "passengerList", 
        .InsertionMode = InsertionMode.Replace,
        .OnSuccess = "hideForm"
}) %>

The hideForm javascript function will be executed if the AJAX call succeeds and will hide the desired div.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文