Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Razor Form submit not working

I am trying to use a simple form to allow authorized users to modify content on select pages on an MVC3 Razor site that I'm building. I am unable to get the edit form to post correctly though.

My model is as follows:

public class WebContent
{
    public virtual UInt32 id { get; set; }
    public virtual String page { get; set; }
    public virtual String section { get; set; }

    [UIHint("tinymce_jquery_full"), AllowHtml]
    public virtual String content { get; set; }
}

My Controller:

    [Authorize]
    public ActionResult Edit(String page, String section)
    {
        WebContent content = _WebContent.GetSection(page,section);
        return View(content);
    }

    [Authorize]
    [HttpPost]
    public ActionResult Edit(WebContent content)
    {
        if (ModelState.IsValid)
        {
            _WebContent.Update(content);
            return View("Index");
        }
        else return View("Index");
    }

And my View:

@model SongbirdsStudios.Models.WebContent
@{
  ViewBag.Title = "Edit '"+Model.page+"'Page Content";
}

<div>
<h2>Edit</h2>
@using (Html.BeginForm())
{

    <fieldset>
        <legend>Page Content</legend>
        @Html.HiddenFor(m => m.id)
        @Html.HiddenFor(m => m.page)
        @Html.HiddenFor(m => m.section)
        <div class="editor-label">
            Content
        </div>
        <div class="editor-field">
            @Html.EditorFor(m => m.content)
        </div>
        <p>
            <input type="submit" value="Update" />
        </p>
    </fieldset>
}
</div>

The view renders correctly and displays the expected elements. The UIHint("tinymce_jquery_full") is getting picked up correctly and the TinyMCE editor appears on the page. But, when the form submits, I get an exception.

System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (content=...)

Everything I've read indicates that the AllowHTML attribute should allow this to post, but it's not for some reason.

I can get around this by adding the [ValidateInput(false)] attribute to the HttpPost controller method. If I do that, then this exception does not occur, but the model still does not get passed to the controller. It just passes null instead. Examining the HttpContext in the debugger indicates that it is passing 4 separate values - one for each property in my model instead of passing the model class back to the controller. I can't figure out what I need to change to make this work correctly.

I'm hoping it's something simple that I missed, and someone with a better eye can see what it is.

like image 671
ASmith Avatar asked Dec 06 '25 08:12

ASmith


1 Answers

So after further investigation into how ASP MVC maps form fields to the model class and examining the HTML emitted to the browser, I found that this was an issue with the name of the property in my WebContent class.

public virtual String content { get; set; }

The TinyMCE editor uses a content variable to define certain characteristics associated with the editor interface. This was apparently causing the HTML 'content' generated by the user input in the editor to not get mapped back to the Model property.

Simply changing the name of the property in the model class (and of course fixing the corresponding database mapping and view references) immediately fixed the problem.

public virtual String web_data_content { get; set; }

Everything else being identical, this worked perfectly with the UIHint and AllowHTML attributes.

like image 144
ASmith Avatar answered Dec 08 '25 20:12

ASmith