MVC Razor Form submit not working

I am trying to use a simple form to allow authorized users to modify content on featured pages in an MVC3 Razor site I am building. I cannot get the correct posting form.

My model looks like this:

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 items. UIHint ("tinymce_jquery_full") gets it right and the TinyMCE editor appears on the page. But, when the form is submitted, 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 be posted, but that's not for any reason.

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

I hope this is something simple that I missed and someone with a better eye can figure out what it is.

+3


source to share


3 answers


So, after further exploring how ASP MVC maps fields to the model class and examining the HTML released in the browser, I found it was a property name issue 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. Apparently this caused the HTML content generated by the user in the editor to not be displayed back to the Model property.



Simply changing the name of the property in the model class (and of course fixing the appropriate database mapping and referencing references) fixed the problem right away.

public virtual String web_data_content { get; set; }

      

Everything else is identical, it worked great with the UIHint and AllowHTML attributes.

+2


source


Add this attribute to the action

[ValidateInput(false)]

      



This should fix your problem.

+2


source


if you are using ie7 it may have some errors

<input type="submit" value="Update" /> 

      

enter button

0


source







All Articles