Search code examples
c#asp.net-mvcwebgridhtml.beginform

mvc4 webgrid cell with form, code won't render <form> tag


I have a webgrid in MVC4, and I want a custom column to have submit buttons - to resend the message to a specific person.

The code of the column's format looks like this:

format: @<text>
@foreach (DataEntity entity in item.Entities)
{
    using (Html.BeginForm("Resend", "Messages", new {MsgId = entity.MsgID }, FormMethod.Post))
    {
        <input Type="submit" value="@entity.PersonName" />
    }
}
</text>

This code was hand-copied from another PC, so ignore any apperant syntax errors.

My problem is that the HTML page doesnt have any form tags, although as you can see I AM using the Html.BeginForm...

Does anyone have any idea why this happens?

Thanks, Avi.


Solution

  • Html.BeginForm doesn't return a string, it writes directly to the page. This is why you don't get the form tags in the column contents.

    The solution would be to write the tags manually.

    Edit:

    <form action="/Messages/Resend" method="post">
        <input type="hidden" name="outgoingMsgId" value="@entity.MsgID" />
        <input type="submit" value="@entity.PersonName" />
    </form>