Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Html.BeginForm Displaying "System.Web.Mvc.Html.MvcForm" on Page

I have a razor view that I added a delete button to inside of an 'if' statement and when the view is rendered in the browser it is displaying "System.Web.Mvc.Html.MvcForm" next to the delete button.

How do I get rid of it?

Here is the code:

<div id="deletestatusupdate">     @if (update.User.UserName.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase))     {         @Html.BeginForm("deleteupdate", "home")         @Html.Hidden("returnUrl", Request.Url.ToString())         <button name="id" value="@update.StatusUpdateId">Delete</button>     } </div> 

Here is how it shows up in the rendered Razor View:

System.Web.Mvc.Html.MvcForm [Delete Button]

pretend that [delete button] is a an actual button, didn't feel like taking a screen shot.

Thank you for your help.

like image 736
Timothy Green Avatar asked Feb 12 '11 00:02

Timothy Green


People also ask

What is @using HTML BeginForm ()) in MVC?

Html. BeginForm is the Html Helper Extension Method that is used for creating and rendering the form in HTML. This method makes your job easier in creating form. Here, is the method to create a form using Html.

Which two objects can you pass as a parameter to the HTML helper Dot BeginForm method?

BeginForm() method has three arguments, these are actionName, controllerName and AjaxOptions.

How do you pass ID in BeginForm?

How do you pass ID in BeginForm? BeginForm("action","controller", new { Id = 12345 }, FormMethod. Post); Reversing the third and fourth parameters will result in the Id being treated as an attribute instead of a route value.

Which of the following writes an opening tag to the response and sets the action tag to the specified controller action and route values?

BeginForm(RouteValueDictionary) Writes an opening <form> tag to the response and includes the route values from the route value dictionary in the action attribute. The form uses the POST method, and the request is processed by the action method for the view.


1 Answers

The recommended way to generate a form is the following:

<div id="deletestatusupdate">     @if (update.User.UserName.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase))     {         using(Html.BeginForm("deleteupdate", "home"))         {             @Html.Hidden("returnUrl", Request.Url.ToString())             <button name="id" value="@update.StatusUpdateId">Delete</button>         }     } </div> 

Alternatively you could do this:

<div id="deletestatusupdate">     @if (update.User.UserName.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase))     {         Html.BeginForm("deleteupdate", "home");         @Html.Hidden("returnUrl", Request.Url.ToString())         <button name="id" value="@update.StatusUpdateId">Delete</button>         Html.EndForm();     } </div> 

The reason why your original approach did not work is because BeginForm() writes directly to the output.

like image 184
marcind Avatar answered Oct 06 '22 17:10

marcind