ASP.NET MVC: Using the same form for Add and Edit when specifying html attributes?

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

Answers


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" } ))
   { %>
<% } % >

Need Your Help

C#.net text filter

c# asp.net shortcode

I want to filter my text to a particular format.

Ob_flush without discarding buffer

php html codeigniter

I have a PHP script that takes a few minutes to finish processing. While the page is still loading, I want to show part of the PHP output as it becomes available, which can be done using ob_start()...