开发者

MVC LinkButton equalivalent

开发者 https://www.devze.com 2022-12-18 22:35 出处:网络
I\'m rewriting a Web Forms application as an exercise to learn some MVC skills. I have a number of LinkButtons on the original application which postback and raise a serverside event that rebinds开发

I'm rewriting a Web Forms application as an exercise to learn some MVC skills.

I have a number of LinkButtons on the original application which postback and raise a serverside event that rebinds开发者_如何学运维 data to a datagrid.

E.g.

Event handlers:

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
}

What I currently have in MVC is this:

     <%= Html.ActionLink("CurrentYr", "Index", 0)%>
     <%= Html.ActionLink("1", "Index", 1)%>

and

    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);
    }

As the ActionLink renders an tag it doesn't postback so my overloaded Index() method isn't called. What are my options for doing this in MVC?


Try changing your action links to:

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

And add HttpVerbs.Get to your second Index action.

Hyperlinks are sent as GET requests. That's ok as long as your action accepts them AND you make sure to add the correct argument to the command line.

You might also want to consider making these AJAX ActionLinks instead -- which could use POST, but would require that you specify where the new content is loaded. The action may also need to change so that it returns a partial view when requested via AJAX so that you're not returning then entire page, but just the updated portion.

0

精彩评论

暂无评论...
验证码 换一张
取 消