0

I have a view where the user can change some settings, its basically a "edit" page. When the user checks a particular value in a radio group i set a hidden field (its a invisible input type=text field), but when i load the page i want that hidden field set from my code. How to do this? JQuery? or can i "findControl" somehow?

This is the "hidden" field:

<div style="display: none">
<input type="text" name="HiddenImageId" id="HiddenImageId" value="" />
</div>

The above hidden field is set from a jquery that executes when a radio-button is clicked. But when I load in "edit" mode I want myself to set the "hidden" field.

Further down my view i load all the radio-buttons:

<% if (file.Id == imageFile.Id)
   { %>
        <input type="radio" checked="checked" name="filename" class="filename" id="<%= file.Id.ToString()%>" />
<% }
   else
   { %>
        <input type="radio" name="filename" class="filename" id="<%= file.Id.ToString()%>" />
 <%} %>

When I set the checked attribute I want to set the value of my hidden fiddle to the files ID.

1
  • Why aren't you setting the type to hidden instead? Like this: <input type="hidden" name="HiddenImageId" id="HiddenImageId" value="" /> You don't need the <div style="display: none">.
    – aolde
    Sep 6, 2009 at 11:22

3 Answers 3

1

You would probably benefit a lot from making better use of the [Html Helpers] in ASP.NET MVC.

You could, for example, output your "hidden" text input like this:

<%= Html.TextBox("HiddenImageId", imageFile.Id) %>

If imageFile can be null, you might want to add a check for that - use shorthand if to make it look nice:

<%= Html.TextBox("HiddenImageId", imageFile != null ? imageFile.Id : "") %>

You could also probably improve your code for the radiobuttons significantly by using Html.RadioButton...

1

just like you are doing

id="<%= file.Id.ToString()%>"

you can do

<input type="text" name="HiddenImageId" id="HiddenImageId" value="<%= file.Id.ToString()%>" />

or whatever the code is to get your value

1
  • This is actually more of what I was trying to do, and I ended up with a solution similar to this (If a field in my hidden area has a value, then I show that hidden area). Quick and dirty. Just the way I like it.
    – NOCARRIER
    May 4, 2012 at 19:19
1

I'd suggest using the HtmlHelper extensions in both cases.

<div style="display: none">
   <%= Html.TextBox( "HiddenImageId",
                     file.Id == imageFile.Id ? file.Id.ToString() : "" ) %>
</div>

<%= Html.RadioButton( "filename",
                      "",
                      file.Id == imageFile.Id,
                      new { @class = "filename", id = file.Id.ToString() } ) %>

or if you wanted to use a hidden input instead, skip the invisible DIV, and use

<%= Html.Hidden( "HiddenImageId", 
                 file.Id == imageFile.Id ? file.Id.ToString() : "" ) %>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.