我有这样的主要观点:
<%@ 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" }
)%&gt;
和这样的控制器:
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);
}
}
}
和这样的部分观点:
&lt;%@ Control Language =“C#”Inherits =“System.Web.Mvc.ViewUserControl”%&gt;
富: 酒吧:&lt;%@ Control Language =“C#”Inherits =“System.Web.Mvc.ViewUserControl”%&gt;
富: 酒吧:我总是通过控制器操作设置值。我想在视图中设置值并传递给局部视图。我怎么能这样做?
答案 0 :(得分:1)
假设您有一个视图模型
public class MyViewModel
{
public string Foo { get; set; }
public string Bar { get; set; }
}
和控制器:
public class FooController: Controller
{
public ActionResult Partial1(string id)
{
// TODO: use the id to fetch the model from somewhere
MyViewModel model = ...
return View(model);
}
public ActionResult Partial2(string id)
{
// TODO: use the id to fetch the model from somewhere
SomeOtherViewModel model = ...
return View(model);
}
}
相应的局部视图:
<%@ Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.MyViewModel>"
%>
<div>Foo: <%= Html.DisplayFor(x => x.Foo) %></div>
<div>Bar: <%= Html.DisplayFor(x => x.Bar) %></div>
最后在主视图中你可以有一个链接:
<%= Html.ActionLink(
"click me",
"Partial1",
"Foo",
new { id = "123" },
new { id = "mylink" }
) %>
<div id="resultpartial1" />
可能是AJAXified:
$(function() {
$('#mylink').click(function() {
$('#resultpartial1').load(this.href);
return false;
});
});
现在当然,如果只有javascript知道id参数,你可以这样做:
<%= Html.ActionLink(
"click me",
"Partial1",
"Foo",
null,
new { id = "mylink" }
) %>
然后:
$(function() {
$('#mylink').click(function() {
$('#resultpartial1').load(this.href, { id: 123 });
return false;
});
});
更新:
第二个链接:
<%= Html.ActionLink(
"click me second link",
"Partial2",
"Foo",
new { id = "123" },
new { id = "mysecondlink" }
) %>
<div id="resultpartial2" />
然后:
$(function() {
$('#mysecondlink').click(function() {
$('#resultpartial2').load(this.href, { id: 123 });
return false;
});
});
答案 1 :(得分:0)
什么似乎是问题?
两个链接的模式应该是(如果你使用jQuery):
$(function(){
// attach link clicking functionality to call controller action via Ajax
$("#LinkID").click(function(evt) { // reference 1 below
evt.preventDefault();
$.ajax({
type: "GET",
url: $(this).attr("href"), // if link has the correct URL
success: function(data) {
$("#containerID").append(data);
},
error: function(xhr, status, err) {
// Handle error ie. alert()
}
});
});
// add code for the second link
});
如果两个链接的工作方式相同,那么您可以在行引用1 中更改选择器以包含其中的两个链接。但是如果它们使用不同的容器,您可以将容器选择器添加为链接的自定义属性,并且仍然只使用单个功能。相应地更改代码。
您可以使用常规
在主视图上放置链接<%= Html.ActionLink("Link text", "action", "controller", null, new { id = "LinkID" }) %>
因此他们将拥有您可以根据上述代码使用的正确网址。使用控制器操作返回需要在客户端上显示的局部视图。