Предполагается, что у вас есть модель вида
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;
});
});
Теперь, конечно, если параметр id известен только через javascript, вы можете сделать это:
<%= 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;
});
});