A way to make Asp.net Web Pages more like Rails MVC?
AFAIK, the Asp.net Web Pages model only supports 开发者_如何学Ca single form post per page. The user input is taken in with:
if (isPost)
{
// code to capture form input
}
However, is it possible to have Asp.net Web Pages behave more like Rails in allowing multiple actions (methods) per page?
I would like to be able to have a user click a button (posting to the same page) which deletes a given record in a db and then refreshes the same page.
Take a look at ASP.NET MVC.
Your premise that the Asp.Net Web Pages model only supports a single form post per page is incorrect. You can have multiple forms. Rather than using the simple IsPost test, you provide a different name attribute to each form's associated submit button, and test to see which one was clicked by examining the Request.Form collection:
@{
if(Request["form1"] == "submit"){
//form1 submitted
}
if(Request["form2"] == "submit"){
//form2 submitted
}
}
...
<form method="post" id="form1">
...
...
<input type="submit" name="form1" value="Submit" />
</form>
<form method="post" id="form2">
...
...
<input type="submit" name="form2" value="Submit" />
</form>
But if you want an MVC framework, as others have said, use ASP.NET MVC.
You can use jQuery and ASP.NET Page Methods to post a request to the server that calls a delete function.
You can use jQuery to post a form with an action attribute that points to delete function. Here is a great code sample to demonstrate something similar. (That example shows an insert, not a delete.) There's another reference to that same example here on SO.
I should add that this answer is meant to complement Mike Brind's answer, which I have upvoted.
精彩评论