Hidden Form Fields?

Sep 23, 2010 at 6:16 AM

Quick question.  What's the best way to generate a hidden form field?  I'd like to prepopulate my form (set the item id to the hidden form field) and onsubmit pass the id of that item back for an update.  Have I overlooked something here?

 

Thanks!

Coordinator
Sep 23, 2010 at 11:37 AM

Are you using the release on the downloads page or the current change set?

Coordinator
Sep 23, 2010 at 12:16 PM
Edited Sep 23, 2010 at 12:17 PM

If you are using the release on the downloads page, you can do this:

var hidden = new TextBox
{
    Title = "My Hidden Field",
    FieldWrapper = "div",
    FieldWrapperClass = "hidden"
};
hidden.GetType().GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(hidden, "some value", null); 

<style type="text/css">
    .hidden
    {
        display:none;
    }
</style>

I admit that's not the most wonderful thing in the world, but it works.

If you are using the current change set (c56cfa1cf461), you can do this:

var hidden = new TextBox
{
    ResponseTitle = "My Hidden Field",
    Value = "some value",
    Template = string.Format(@"<div style=""display:none;"">{0}</div>", PlaceHolders.Input)
};

The problem with both of these solutions is that they render a textbox input element instead of a real hidden input element. That may be fine for your needs. I will go ahead and implement a true hidden input field type.

I think people are using this library in ways that it was not intended, which is a great thing. I'll reply when I check in that change.

Coordinator
Sep 23, 2010 at 1:09 PM

I added the Hidden field type:

http://mvcdynamicforms.codeplex.com/SourceControl/changeset/changes/ef8fb2ed1afb

It derives from InputField, so it has many properties that are sort of irrelevant. For example, I assumed that this field type should always be valid, so the Error and ErrorClass properties don't have any use. Also, Prompt has no use. The RenderHTML() method will only ever replace the Input placeholder, so remember that if you use a custom template.

Someone let me know if the Hidden field could be improved.

Sep 23, 2010 at 2:53 PM

Ok thanks.  I'm using the release, not the latest code (I'm still on VS 2008) but look forward to using the latest code whenever we upgrade at work. 

Is the hidden form field the right approach for updates though?  I'm wondering what other people have used here?  Querystring on the form action or ID param in the action method?

Thanks!

Coordinator
Sep 23, 2010 at 3:19 PM

Sorry, I don't understand your question.

Sep 23, 2010 at 5:24 PM

No worries.  I was going back to my initial thought on my original question - If I prepopulate a form from an existing object that has an ID value and I want to edit and save the changes, what is the best approach to do this.  My first thought as posted above was to render the form w/ the values set and create a hidden form field that contains the object's ID.  Then when I click submit instead of creating a new record I would obtain the ID, load that object by the ID and update it's properties from the form object.  After sleeping on it, I realized instead of using a hidden form field I could modify the action url on the form and add the id as a querystring parameter.  My question was to see how other people using your application have approached this situation.