Reputation: 9917
I wish to use the same form for adding and editing records within a database using a partial view. I understand that this is fine as the standard Html.BeginForm automatically output the correct html depending on the action that is being used (Add / Edit). However, I need to out said form with some extra HTML attributes. There does not appear to be an overload that allows this to happen without also specifying the ACTION and CONTROLLER names. If I hardcode these then surely I cant use the same form for edit and add automatically?
Or am I missing something?
CHeers
Upvotes: 2
Views: 214
Reputation: 532465
Set the values of the action and controller to null and they will be pulled from the context. If you look at the source for the overload that doesn't require parameters, you'll see that it simply calls the versions requiring more parameters with these values as null. Calling that overload directly with null values will have the same effect. Depending on the overload you're using you might need to cast the null values as strings -- I'd do this only if the compiler is not able to differentiate the methods without casting the nulls as strings.
<% using (Html.BeginForm( null, null, FormMethod.Post, new { @class = "foo" } ))
{ %>
<% } % >
Upvotes: 2