2010-02-04 93 views
1

我正在重写Web窗体应用程序,作为练习来学习一些MVC技能。MVC LinkBut​​ton等价

我在原始应用程序上有多个LinkBut​​tons,它们会回发并引发将数据重新绑定到数据网格的serverside事件。

E.g.

事件处理程序:

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的呈现它不是一个标签回发所以我重载的Index()方法不被调用。我在MVC中做这些的选择是什么?

回答

1

试着改变你的动作链接:

<%= Html.ActionLink("CurrentYr", "Index", new { offset = 0 })%> 
<%= Html.ActionLink("1", "Index", 1, new { offset = 1 })%> 

并添加HttpVerbs.Get你的第二个索引操作。

超链接作为GET请求发送。没关系,只要你的动作接受它们,并且确保将正确的参数添加到命令行。

您可能还想考虑制作这些AJAX动作链接 - 它可以使用POST,但会要求您指定新内容的加载位置。该动作可能还需要进行更改,以便在通过AJAX请求时返回部分视图,以便不返回整个页面,而只是返回更新的部分。

+0

看起来像你错过了“,”1和新的。 – 2010-02-04 15:22:17

+0

@Nitin:谢谢。我修复了错字。 – tvanfosson 2010-02-04 16:04:40

+0

这很有用,谢谢。我将研究AJAX ActionLinks。 – 2010-02-04 16:11:49