.NET MVC Ajax 表单 - 如何隐藏它?
好吧,一切都“功能性”地与我试图完成的任务一起工作,我再次确信这是愚蠢的事情,但我不知道如何做这一件事。
我有一个实体的编辑表单,比如说一辆汽车。这辆“汽车”可以有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧 - 想通了,我想昨晚已经太晚了。所以,这就是答案,它实际上是一些小问题最终汇聚成的。
再次感谢达林的帮助,我试图从中获得比结果更多的东西。
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.
Thanks again darin for your assistance, I was trying to making more out of it than it turned out to be.
确保您已在页面中引用了
MicrosoftAjax.js
和MicrosoftMvcAjax.js
脚本。获取部分视图内容(白屏、文本)的原因可能是脚本错误,导致 ajax 无法正确执行。使用FireBug查看执行过程中是否出现错误。至于隐藏表单,您可以使用
OnSuccess
回调:如果 AJAX 调用成功,将执行
hideForm
javascript 函数,并隐藏所需的 div。Make sure that you've referenced both the
MicrosoftAjax.js
andMicrosoftMvcAjax.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:The
hideForm
javascript function will be executed if the AJAX call succeeds and will hide the desired div.