In ASP.NET MVC, how do I get the mangled name of a control when a form is submitted?

I've got a form inside an <asp:Content> block that is being submitted to a controller. For one of the controls, I need to get some information from it directly that won't happen automatically by calling UpdateModel().

However, in the Request.Form dictionary, the control's id is of the mangled form ctl00$ContentPlaceHolder${name}. Given that I'm in the controller, and know nothing about the view at this point, what is the proper way of accessing the control's data?

Here is what the view (.aspx) looks like (removed extraneous code):

<%@ Register Assembly="FredCK.FCKeditorV2" Namespace="FredCK.FCKeditorV2" TagPrefix="FCKeditorV2" %>

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" AutoEventWireup="true"
    CodeBehind="...." Inherits="...." %>

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder" runat="server">

    <form id="form1" action="..." method="post">

            <FCKeditorV2:FCKeditor ID="AuthorBio" runat="server" Height="250"/>
            <input type="submit" value="Save" />
    </form>

</asp:Content>

The control named AuthorBio shows up in the controller in the Form.Request dictionary as ctl00$ContentPlaceHolder$AuthorBio$

The reason I'm trying to use the 3rd-party control with "runat-server" is because I need to set the editor's Value as follows:

AuthorBio.Value = HttpUtility.HtmlDecode(ViewData.Model.Bio);

Trying to do this in the .aspx file in the FCKeditorV2 tag doesn't work. (Or maybe I'm missing something there too)

Ok, so the key is to use the JavaScript version of the editor rather than the wrapped control. There was also a handy comment that I'm going to include here to accompany the accepted answer:

you should use the javascript version of the FCKEditor control not the .NET custom control as the .NET custom control was built on the WebForms paradigm. The JS version should have a hidden field for the value of the Html which you can access in your controller using Request["FieldName"]

Answers


FCKEditor is a standard javascript library that also comes wrapped in an ASP.NET control for Webforms. So it would be easier to use the FCKEditor javascript without the ASP.NET control. It will be easier to integrate into MVC that way.

If you must use the ASP.NET control version then you will have these kinds of issues. But one solution is to place the FCKeditor on a standard webforms page and then show that in a dialog using something like Greybox.


Because the name mangling depends on how the control is used I think you'll have to search for it. Make sure that it has a unique name, then iterate through the Request.Form (or FormCollection if using this as a parameter) and look for a key that ends with "$ControlName." When you find that name, then use that key to get at the value.

string editorKey = null;
foreach (string key in Request.Form.Keys)
{
    if (key.EndsWith( "$AuthorBio" ))
    {
       editorKey = key;
       break;
    }
}

if (!string.IsNullOrEmpty( editorKey ))
{
    ... process
}

Are you using a WebForm control? To my knowledge, the default ASP.NET MVC view engine does not mangle control ID's.


Traditional ASP.NET: runat="server" ASP.NET MVC = no "runat=server" controls.

You are not using MVC correctly. The fact that you want to use "runat=server" controls means that you should be doing a traditional ASP.NET app.

This is a semi-old article, but you might want to check it out to get the difference between ASP.NET and MVC: http://www.singingeels.com/Articles/ASPNET_MVC_in_the_Real_World.aspx


One option is to write some javascript behind the submit button that will set the form post variable to what you want it to be. You could use a hidden field that you update on change of your AuthorBio or you could set it programatically on the submission.


Need Your Help

Strophe.js MUC: creating a room and joining more than one room

javascript strophe

I'm creating a chat website and I'm using Strophe.js and the Strophe.muc.js plugin. The single chat functionalities work fine, but I also wan't to implement a group chat function where users can cr...

Access joomla's helpers from outside php file

php joomla libraries helpers dbo

I'm trying to access joomla's methods from a php file on the same server.