Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Quick and dirty:</p> <pre><code>&lt;% foreach(string language in ViewData["Languages"] as string[]) { %&gt; &lt;%= Html.RadioBox(language,language,language == ViewData["UserLanguage"].ToString()) %&gt; &lt;% } %&gt; </code></pre> <p>The problem is that you have allot of magic strings in that viewdata. What I would do is something like this:</p> <ol> <li><p>Create a class: UserForm</p> <pre><code>class UserForm { IList&lt;string&gt; _languages; string _selectedL; public UserForm (IList&lt;string&gt; languages, string selectedLanguage) { _languages = languages; _selectedL = selectedLanguage; } IEnumerable&lt;SelectedListItem&gt; UserLanguages { get { return from l in _languages select new SelectedListItem { Text = l, Value = l, Selected = (l == _selectedL) }; } } } </code></pre></li> <li><p>the view should be strongly typed and be of type : UserForm then you could render it like : </p> <pre><code>&lt;%= Html.RadioButtonList("userLanguages",ViewData.Model.UserLanguages) %&gt; </code></pre></li> <li><p>From the controller you would : </p> <pre><code>return View(new UserForm(listOfLanguages, selectedLanguage)); </code></pre></li> </ol> <p>HTH.</p> <p><strong>EDIT:</strong> OK, found it - the <code>RadioButtonList</code> is an extension method in the <code>Microsoft.Web.Mvc</code> namespace (the MVCFutures project) - you can get it from here : <a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471" rel="nofollow noreferrer">MVCFutures</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload