tags:

views:

749

answers:

5

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"]

A: 

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

Todd Smith
What does that mean? I tried replacing <form ...> with <%=Html.BeginForm.. %> but it doesn't make any difference.
Jedidja
The "ASP.NET MVC view engine" ... is the ASP.NET ENGINE... it's not something different. It "mangles" ID's the same, because it is the same!
Timothy Khouri
You can replace the "view engine" in ASP.NET MVC. Google "asp.net mvc view engine" for more info.
Todd Smith
A: 

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.

Odd
A: 

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

Timothy Khouri
Could you provide some more details? I'm using the FCKEditor control, which is not an "MVC" control, and need to set some properties on it that (seemingly) cannot be done automatically through the model. For instance, in OnLoad, I have to set the editor text to a value contained in the model.
Jedidja
'runat=server' controls are part of traditional ASP.NET... they use the ViewState and other things that are not compatible with MVC. You can't mix both worlds, and I'm having a hard time understanding why you want to MVC if you want to use third party 'traditional ASP.NET' controls.
Timothy Khouri
The rest of the website is running quite nicely with MVC. I'm just trying to use the FCKEditor as I don't want to write one from scratch :)
Jedidja
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"]
+6  A: 

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.

Trevor de Koekkoek
Ah ok, That makes sense.
Jedidja
A: 

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
}
tvanfosson