Click here to Skip to main content
15,936,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a model and each model derives from a base class, that stores a list of translations.

C#
public abstract class BaseObject
{
  public List<LocalizationPart> Translations { get; set; }
}

public class LocalizationPart
{
  public string Culture { get; set; }
  public List<Translation> Translations { get; set; }
}

public class Translation
{
  public string Property { get; set; }
  public string Text { get; set; }
}

public class CivilStatus : BaseObject
{
  public string Name { get; set; }
  public string Description { get; set; }
}


this Translations list gets filled during the get or create of an object.

Now I have an edit screen and from this translations list, I get the translations that I need. (Example: on the object CivilStatus, I can have a name and description property, so each property can have a translation per language.) So in the create and edit page I list the appropriate edit fields below the @Html.Label(...).

C#
<div class="editor-label">
  @Html.Label(T(property.Name.
</div>
<div class="editor-field">
  @if (Attribute.IsDefined(property, typeof(LocalizableAttribute)))
  {
    foreach (var translation in Model.Translations)
    {
       @Html.Label(translation.Culture, new {@class = "vt-cultureid"})
       var translation = translation.Translations.SingleOrDefault(t => t.Property == property.Name);
       if (contentItemVersionRecord != null)
       {
         @Html.EditorFor(m => contentItemVersionRecord.Translation)<br />
       }
    }
  }
</div>


But when I press the save button, the translations list is null when I look at it in the HttpPost action.

C#
[HttpPost]
public ActionResult Create(CivilStatus civilstatus) { ... }


How can I make it that the translations are bound to the model?
Posted
Updated 4-Nov-13 21:36pm
v2
Comments
Sergey Alexandrovich Kryukov 4-Nov-13 9:59am    
You model looks like a 3-level composition tree with a BaseObject in root, but you this class is abstract. So, you cannot have any instances of the model, unless you derive a non abstract descendant from this root class. Your question and sample is at least incomplete.
—SA
KenBonny 5-Nov-13 3:35am    
You are right and I will update my question.

1 solution

To do it right, I had to iterate over the two lists with a for loop, not a foreach loop. If I use

C#
@Html.TextBoxFor(m => m.Translations[i].Translations[j].Translation)


It does pass it correctly.

To allow for the interfaces to be used, I had to create my own model binder.

More information can be found in this article:The Features and Foibles of ASP.NET MVC Model Binding[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900