作为学习一些MVC技能的练习,我正在重写一个Web窗体应用程序。
我在原始应用程序上有许多LinkButtons,它们回发并引发将数据重新绑定到datagrid的服务器端事件。
例如。
事件处理程序:
protected void lbtnOffset0_Click(object sender, EventArgs e)
{
Session["Offset"] = 0;
DataBind(); //this rebinds the data using the above argument
}
protected void lbtnOffset1_Click(object sender, EventArgs e)
{
Session["Offset"] = lbtnOffset1.Text;
DataBind(); //this rebinds the data using the above argument
}我目前在MVC中所做的是:
<%= Html.ActionLink("CurrentYr", "Index", 0)%>
<%= Html.ActionLink("1", "Index", 1)%>和
public ActionResult Index()
{
return View(MietController.GetMietByYearOffset(0);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(int? offset)
{
int offsetValue = offset ?? 0;
return View(MietController.GetMietByYearOffset(offsetValue);
}当ActionLink呈现一个标记时,它不会回发,所以我的重载索引()方法不会被调用。在MVC中我有什么选择呢?
发布于 2010-02-04 23:19:03
尝试将您的操作链接更改为:
<%= Html.ActionLink("CurrentYr", "Index", new { offset = 0 } )%>
<%= Html.ActionLink("1", "Index", 1, new { offset = 1 } )%> 并将HttpVerbs.Get添加到第二个索引操作中。
超链接作为GET请求发送。只要您的操作接受它们,并且确保在命令行中添加了正确的参数,这就没问题。
您可能还想考虑改为使用这些AJAX ActionLinks --这可以使用POST,但需要指定加载新内容的位置。该操作可能还需要更改,以便在通过AJAX请求时返回部分视图,这样您就不会返回整个页面,而只返回更新后的部分。
https://stackoverflow.com/questions/2200724
复制相似问题