Let’s say you have this Model:
//model
public class Stuff
{
public string Name { get; set; }
public Dictionary<String, String> Description { get; set; }
}
I want to be able to be able to create an action and it’s corresponding view so users can add in the form the Name of the Stuff object, and can add multiple Description entries.
In this particular case, I want the key to be a language code like ‘en’, ‘de’, ‘fr’, ‘es’, etc. and the description the corresponding description for the given language.
For example, in the View you might see something like this:
@model Stuff
@using(Html.BeginForm())
{
<div>
@Html.LabelFor(x=>x.Name)
@Html.TextBoxFor(x=>x.Name)
</div>
<div>
<!-- What goes in here to map to the Dictionary in the Stuff Model? -->
<input name="LanguageCode" value="en" /> <input name="DescriptionValue" />
<input name="LanguageCode" value="de" /> <input name="DescriptionValue" />
<input name="LanguageCode" value="fr" /> <input name="DescriptionValue" />
</div>
<div>
<input type="submit" value="save" />
</div>
}
// controller
[HttpGet]
public ActionResult Index ()
{
return View(new Stuff());
}
[HttpPost]
public ActionResult Index (Stuff myStuff)
{
foreach(KeyValuePair kvp in myStuff.Description)
{
Trace.WriteLine(String.Format("Language: {0} - Description: {1}", kvp.Key, kvp.Value));
}
DBHelper.Save(myStuff);
return View();
}
Any alternative solutions are accepted.
Thanks.
It will be something like this:
See this post by Scott Hanselman