Doesnt work after upgrade to mvc5 with razor view

Mar 4, 2014 at 5:38 PM
Hello,
I upgraded the project on my machine to mvc5 and razor view engine. After some changes in RenderHtml (return as HtmlString) the form renders correctly.

However when i post the form the contextof form is lost
var form = (Form)bindingContext.Model;

I get an error as below.
if (form == null)
            throw new NullReferenceException("The dynamic form object was not found. Be sure to include PlaceHolders.SerializedForm in your form template.");   
*__ Controller__

public ActionResult Demo3()
    {
        // recreate the form and set the keys
        var form = FormProvider.GetForm();
        Demo3SetKeys(form);

        // set user input on recreated form
        UpdateModel(form);

        if (Request.HttpMethod == "POST" && form.Validate()) // input is valid
            return View("Responses", form);

        // input is not valid
        return View("Demo", form);
    }

    void Demo3SetKeys(Form form)
    {
        int key = 1;
        foreach (var field in form.InputFields)
        {
            field.Key = key++.ToString();
        }
    }
  • View
@inherits System.Web.Mvc.WebViewPage<MvcDynamicForms.Form>
@{
ViewBag.Title = "Demo";
}
<h2>Demo</h2> <script type="text/javascript"> $(function () { // hide error labels when the user clicks on the offending input element $(':input').focus(function () { $(this).closest('td').children('label.MvcDynamicFieldError').fadeOut(); }); }); </script> @using (Html.BeginForm("", "", FormMethod.Post, new {enctype = "multipart/form-data" ,@class = "form-horizontal", role = "form" }))
{
@Model.RenderHtml(true)
<input type="submit" />
}
  • Changes:
    public System.Web.HtmlString RenderHtml(bool formatHtml)
    {
        var fieldsHtml = new StringBuilder();
        foreach (var field in _fields.Where(x => x.Display).OrderBy(x => x.DisplayOrder))
            fieldsHtml.Append(field.RenderHtml());
    
        var html = new StringBuilder(Template);
        html.Replace(PlaceHolders.Fields, fieldsHtml.ToString());
    
        if (Serialize)
        {
            var hdn = new TagBuilder("input");
            hdn.Attributes["type"] = "hidden";
            hdn.Attributes["id"] = MagicStrings.MvcDynamicSerializedForm;
            hdn.Attributes["name"] = MagicStrings.MvcDynamicSerializedForm;
            hdn.Attributes["value"] = SerializationUtility.Serialize(this);
            html.Replace(PlaceHolders.SerializedForm, hdn.ToString(TagRenderMode.SelfClosing));
        }
    
        html.Replace(PlaceHolders.DataScript, RenderDataScript(null));
    
        PlaceHolders.RemoveAll(html);
    
        if (formatHtml)
            return new System.Web.HtmlString(XDocument.Parse(html.ToString()).ToString());
    
        return new System.Web.HtmlString(html.ToString());
    }
regards
    }
Coordinator
Mar 4, 2014 at 6:15 PM
I'm sorry but I simply don't have time to troubleshoot this. You are welcome to submit a pull request if you fix the problem.