Form tags being stripped out

Topics: Troubleshooting
Jul 23, 2012 at 9:47 PM
Edited Jul 23, 2012 at 9:48 PM

Hi, I'm sure there is a very good reason for this, but I cant seem to figure out how to add a form element to the edit page of an item in the dashboard.

What I am trying to do is this:

I have two entities, Venues and Events. Both these can be managed through the dashboard. However, I want the user to be able to add events to a venue when viewing the venue edit page. So on the venue edit template I have a little subsection in the html that contains the form input elements for an event and a submit button all inside a form tag. The form tag has a post action to a controller that I have created.

So here is a list of issues that are probably all related.

  1. When using Html.BeginForm or Ajax.BeginForm, no input elements get rendered (only text and buttons). So I just added the actual form tags manually.
  2. The prefix of my venue driver automatically gets added to the event input elements, so model binding doesn't work.
  3. A anti forgery exception is thrown. This is fixed when I use Html.BeginFormAntiForgeryPost(), but that in turn removes the form tags (this time the content, i.e. the input elements, remains intact)

Here is the source:


@using EventManagement.ViewModels
@using Orchard.Core.Title.Models
@using Orchard.ContentManagement
@model  VenueEditViewModel
<fieldset class="custom-edit-styles">
    @if (Model.Id > 0) @* Only display this if the venue has been persisted *@
        <div class="venue-events-edit">            
            <div class="add-new">
                <form method="post" action="/Event/QuickAdd">
                    @using (Html.BeginFormAntiForgeryPost())
                        @Html.LabelFor(m => m.EventQuickAdd.Name)
                        @Html.TextBox("Name", null, new { data_val = "true", data_val_required = "A name is required" })
                        <span class="validation-message" data-valmsg-for="Venue.Name"></span>
                        <br class="clr" />
                        @Html.LabelFor(m => m.EventQuickAdd.Date)
                        @Html.TextBox("Date", System.DateTime.Now.ToString("dd/MM/yyyy"), new { data_evnt_datepicker = "true" })
                        <br class="clr" />
                        @Html.LabelFor(m => m.EventQuickAdd.StartTime)
                        @Html.TextBox("StartTime", "12:00", new { data_evnt_timepicker = "true" })
                        <br class="clr" />
                        @Html.LabelFor(m => m.EventQuickAdd.EndTime)
                        @Html.TextBox("EndTime", "13:00", new { data_evnt_timepicker = "true" })
                        <br class="clr" />
                        @Html.LabelFor(m => m.EventQuickAdd.Spaces)
                        @Html.TextBox("Spaces", Model.MaxCapacity.ToString())
                        @Html.Hidden("VenueId", Model.Id.ToString())
                        <br class="clr" />
                        <br class="clr" />
                        <button type="submit" class="button primaryAction">
                            Quick Add</button>
            <br class="clr" />
    <div class="venue-details-edit">
        <strong>@T("Venue Details")</strong>
        -- some more venue specific input fields here --
@using (Script.Foot())
        $(function () {
            $('.add-new form').submit(function () {
                if ($(this).valid()) {
                        url: '/Event/QuickAdd',
                        type: 'POST',
                        data: $(this).serialize(),
                        success: EventQuickAddSuccess
                return false;
        function EventQuickAddSuccess(data) {



    public class EventController : Controller
        private IContentManager ContentManager;
        public EventController(
            IContentManager contentManager)
            ContentManager = contentManager;

        public JsonResult QuickAdd(VenueEventQuickAddViewModel model)
            string startTimeStr = model.StartTime.Replace("h",":").Replace("-",":");
            string endTimeStr = model.EndTime.Replace("h",":").Replace("-",":");
            string dateStr = model.Date.ToString("yyyy-MM-dd");
            DateTime startDateTime = DateTime.MinValue;
            DateTime endDateTime = DateTime.MinValue;
            if (!(DateTime.TryParse(dateStr+" "+startTimeStr, out startDateTime)
                DateTime.TryParse(dateStr+" "+endTimeStr, out endDateTime)))
                return Json(new {success = false, message="The dates are in the incorrect format" });
            if (model.VenueId<=0)
                return Json(new {success = false, message="No venue selected" });
            var evt = ContentManager.New<EventPart>("Event");
            evt.Venue = ContentManager.Get<VenuePart>(model.VenueId).Record;
            evt.Closed = false;
            evt.StartDateTime = startDateTime;
            evt.EndDateTime = endDateTime;
            evt.Spaces = model.Spaces;
            ContentManager.Create(evt, VersionOptions.Published);
            bool success = evt.Id > 0;
            return Json(new { success=success, message = "Event added successfully" });



Thank you very much for all the help.

Jul 23, 2012 at 10:13 PM

Please tell me if I'm going about this in a complete wrong way. Is there a better way to achieve the same features?

Jul 28, 2012 at 6:05 PM
Edited Jul 28, 2012 at 6:05 PM

The problem is that your form element is nested within another form element that is rendered by the Contents module. So you should really not use form elements in your part editor templates. One alternate solution could be to create your own controller. You could then still render the content editor shapes as the Contents module does, but you wouldn't have to use forms inside of your part editor templates; you can handle specific scenario's from within your own controller.

Another approach may be using AJAX to post your data to your own controller.