Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to use muliple CRUD(Create,Read,Update,Delete) actions on one Razor view form in MVC3.And therefore i can only declare my action names on my controller
to different actions and have thier implementations there.
I assume something like this..
JavaScript
@using(Html.BeginForm("ControllerName","ModelName",[List of actions])

....
....



On My Controller.cs

C#
[HttpPost]
[ActionName("Create")]

I think we can also have custom html helpers and do this.But i am not sure how to proceed further with this.

Can any one help me with this?I am looking something more simpler way to do.
I have googled,but most of the solutions looks complex. :-)

Thanks,
Keerthi
Posted
Updated 16-Aug-12 10:36am
v2

There is no ready-made helper that builds this for you. Since it depends on your business needs how the actions will be presented. The simplest way is using many forms.

There is no problem placing many forms on a web page. Classic ASP.NET did not allow this in general, but the html standard has nothing against it. So if you want all your actions to be post actions, just add all as simple forms containing just a submit link (or button).

C#
@using(Html.BeginForm("fooController", "DeleteAction", FormMethod.Post, new { id = model.id }))
{
   <input type="submit" value="Delete" />
}

and so on...
 
Share this answer
 
Comments
SKPR 2012 17-Aug-12 4:04am    
hi Zoltan,
Thanks for prompt reply.
But i am looking for a solution where i could have multiple actions defined in one form rather than having seperate forms for each action.
It is ok to have a custom html helper for this.I just need help in building a custom html helper which defines mutlple action in a single form.

Thanks
Zoltán Zörgő 17-Aug-12 4:31am    
The "action" is an MVC concept, not html or http (the form action is not the same, even if related). At the end on client side you will have only html elements, and http traffic. Thus you have to fit your needs in these standards. I understand, what you need, but you can not break the limits. Any html helper will build a html string at the end. But I think this is not the best approach.
You want something simple, but you don't want to accept simple approach. All that remains is a complex solution.

There are some approaches, if you want a single form:
1) You can write a custom action selector attribute
2) Use the action selector attribute described here: http://blog.ashmind.com/2010/03/15/multiple-submit-buttons-with-asp-net-mvc-final-solution/
3) Change form action property before submit
This is bit confusing.

First, your following code is wrong

C#
@using(Html.BeginForm("ControllerName","ModelName",[List of actions])


BeginForm takes (ActionName,CntrollerName,Method,Parameters) and not the ModelName,

if this is a typo then, in CRUD, you only need Form in HTML where you use HTTP_POST, and Read and Delete are HTTP_GET, so you do not need form for these.

For Create and Update you need form, now I do not understand why you need Create and Update Form on same view ?

Suppose you want to do this anyway, either you can use multiple forms using BeginForm or,

you can create Partial Views for Create and Update, and then render them on your main View using @Html.Action(Controller,PartialView/ActionName). if you do this you do not need any form at all on your main view. this way you will use those partial views like user controls in ASP.Net Webforms.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900