ASP.NET MVC 2: Html.ActionLink another function within the controller?
I'm trying to figure out how controllers work. Obviously when a page first loads, the 开发者_如何学运维ActionResult Index()
is called. But how do you call other function within the controller?
I tried using Html.ActionLink("Click Me", "ControllerFunction")
but it just looked for a page called "ControllerFunction."
What I'd like to be able to do is call a function within the controller that updates the view data and then refreshes the page (or Ajax).
How is this supposed to work? Because right now the only useful function within the controller is Index()
Controller:
public HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult AjaxAction()
{
return View();
}
}
and then in the Index.aspx view you could have a link and an empty div which will harbor the result of the AJAX request:
<%: Html.ActionLink("ajax test", "ajaxaction", null,
new { id = "ajaxtest" })%>
<div id="result"></div>
which you would then enhance with javascript (jquery in my case):
$(function() {
$('#ajaxtest').click(function() {
$('#result').load(this.href);
return false;
});
});
When you click on the link an AJAX request will be sent to /home/ajaxlink
which would render a partial view AjaxAction.ascx
and insert its contents into the result div.
you clearly are missing the basics of MVC :)
have a look at this great introduction to MVC: http://channel9.msdn.com/blogs/matthijs/aspnet-mvc-2-basics-introduction-by-scott-hanselman
精彩评论