我如何动态加载部分视图?
我的意思是我有这个观点,让我们说ListProducts,在那里我选择一些下拉列表与产品等,并从我想要填充部分视图的选定的值,这将是一个div是不可见,但在onchange()事件将变得可见,并显示来自特定选定项目的数据。
解决方法
使用jQuery的
$.load()与控制器动作返回部分视图。
例如:
例如:
HTML
<script type="text/javascript"> $(document).ready(function() { $("#yourselect").onchange(function() { // Home is your controller,Index is your action name $("#yourdiv").load("@Url.Action("Index","Home")",{ 'id' : '123' },function (response,status,xhr) { if (status == "error") { alert("An error occurred while loading the results."); } }); }); }); </script> <div id="yourdiv"> </div>
调节器
public virtual ActionResult Index(string id) { var myModel = GetSomeData(); return Partial(myModel); }
视图
@model IEnumerable<YourObjects> @if (Model == null || Model.Count() == 0) { <p>No results found</p> } else { <ul> @foreach (YourObjects myobject in Model) { <li>@myObject.Name</li> } </ul> }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。