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 ?
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: