Ajax 请求获取部分视图
我的主视图如下:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
指数
<script src="../../Scripts/jquery-1.3.2.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<h2>
Index</h2>
<script type="text/javascript">
$(function() {
$('#mylink').click(function() {
$('#resultpartial1').load(this.href);
return false;
});
});
$(function() {
$('#mysecondlink').click(function() {
$('#resultpartial1').load(this.href, { id: 123 });
return false;
});
});
</script>
<%= Html.ActionLink("click me","Partial1","Foo",new MyViewModel { Foo = "123" , Bar="456" },new { id = "mylink" }) %>
<%= Html.ActionLink("click me second link","Partial2", "Foo", new { id = "123" }, new { id = "mysecondlink" }
) %>
和这样的控制器:
public class FooController : Controller
{
//
// GET: /Foo/
public ActionResult Index()
{
return View();
}
public ActionResult Partial1(string id)
{
// TODO: use the id to fetch the model from somewhere
MyViewModel model = new MyViewModel { Bar = "a", Foo = "1" };
return View(model);
}
public ActionResult Partial2(string id)
{
// TODO: use the id to fetch the model from somewhere
MyViewModel model = new MyViewModel { Bar = "b", Foo = "2" };
return View(model);
}
}
}
和这样的部分视图:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
Foo: Bar:<%@ 控制语言="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
Foo: Bar:我总是获取控制器操作设置的值。我想在视图中设置值并传递到部分视图。我该怎么做?
I have my main view like this:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
Index
<script src="../../Scripts/jquery-1.3.2.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<h2>
Index</h2>
<script type="text/javascript">
$(function() {
$('#mylink').click(function() {
$('#resultpartial1').load(this.href);
return false;
});
});
$(function() {
$('#mysecondlink').click(function() {
$('#resultpartial1').load(this.href, { id: 123 });
return false;
});
});
</script>
<%= Html.ActionLink("click me","Partial1","Foo",new MyViewModel { Foo = "123" , Bar="456" },new { id = "mylink" }) %>
<%= Html.ActionLink("click me second link","Partial2", "Foo", new { id = "123" }, new { id = "mysecondlink" }
) %>
and controller like this:
public class FooController : Controller
{
//
// GET: /Foo/
public ActionResult Index()
{
return View();
}
public ActionResult Partial1(string id)
{
// TODO: use the id to fetch the model from somewhere
MyViewModel model = new MyViewModel { Bar = "a", Foo = "1" };
return View(model);
}
public ActionResult Partial2(string id)
{
// TODO: use the id to fetch the model from somewhere
MyViewModel model = new MyViewModel { Bar = "b", Foo = "2" };
return View(model);
}
}
}
and partial views like this :
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
Foo:
Bar:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
Foo:
Bar:
I am always getting values set by controller actions. I want to set values in view and pass to partial view. How can I do this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设你有一个视图模型
和一个控制器:
一个相应的部分视图:
最后在你的主视图中你可以有一个链接:
它可以是 AJAX 化的:
现在当然,如果 id 参数仅通过 javascript 知道,你可以这样做:
和然后:
更新:
对于第二个链接:
然后:
Assuming you have a view model
and a controller:
a corresponding partial view:
and finally in your main view you could have a link:
which could be AJAXified:
Now of course if the id parameter is known only with javascript you could do this:
and then:
UPDATE:
And for the second link:
and then:
似乎有什么问题?
包含两个链接的模式应该是(如果您使用 jQuery):
如果两个链接的工作方式相同,那么您可以更改 reference 1 行中的选择器以将两个链接包含在其中。但是,如果它们使用不同的容器,您可以将容器选择器作为自定义属性添加到链接中,并且仍然仅使用单个功能。相应地更改代码。
您可以使用通常的方式在主视图上放置链接,
这样它们就会有正确的 URL,您可以按照上面的代码使用。使用控制器操作返回您需要在客户端上显示的部分视图。
What seems to be the problem?
Pattern with both links should be (if you use jQuery):
If both links work the same, then you could change selector in line reference 1 to include both links in it. But if they work with a different container, you could add container selector as a custom attribute to the link and still use just single functionality. Change the code accordingly.
You could place links on your main view using the usual
so they will have correct URL that you can use as per code above. Use controller action that returns the partial view you need to display on the client.