views:

20

answers:

1

I've got this object created from EntityFramework from my database.

[EdmEntityTypeAttribute(NamespaceName="ContactCoreModel", Name="TargetLang")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class TargetLang : EntityObject
{
    #region Factory Method

    /// <summary>
    /// Create a new TargetLang object.
    /// </summary>
    /// <param name="idTarget">Initial value of the idTarget property.</param>
    /// <param name="idLang">Initial value of the idLang property.</param>
    /// <param name="name">Initial value of the name property.</param>
    public static TargetLang CreateLinguaTarget(global::System.Int32 idTarget, global::System.Int32 idLang, global::System.String name)
    {
        TargetLang targetLang = new TargetLang();
        targetLang.idTarget = idTarget;
        targetLang.idLang = idLang;
        targetLang.name = name;
        return targetLang;
    }

    #endregion

    [...]

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.String name
    {
        get
        {
            return _nome;
        }
        set
        {
            OnnomeChanging(value);
            ReportPropertyChanging("name");
            _nome = StructuralObject.SetValidValue(value, false);
            ReportPropertyChanged("name");
            OnnomeChanged();
        }
    }
}

In my Create View I'll do something like

<% using (Html.BeginForm()) { %>
  <% foreach (var lang in Env.ActiveLangs) { %>
    <fieldset>
      <legend>Data for language <%: lang.name %></legend>
      <% var targetLang = Model.targetsLangs.FirstOrDefault(lt => lt.idLang.Equals(lang.id)) ?? new TargetLang(); %>
      <div class="editor-label">
        <%: Html.Hidden("targetLangs.Index", lang.id)%>
        <%: Html.Hidden("targetLangs[" + lang.id + "].idLingua", lang.id)%>
        <%: Html.Hidden("targetLangs[" + lang.id + "].idTarget", Model.id)%>
        <%= Html.Label("targetLangs_" + lang.id + "__name", "name")%>
      </div>
      <div class="editor-field">
        <%: Html.TextBox("targetLangs[" + lang.id + "].name", targetLang.name)%>
        <%: Html.ValidationMessage("targetLangs[" + lang.id + "].name")%>
      </div>
    </fieldset>
  <% } %>
  <p>
    <input type="submit" value="Create" />
  </p>
<% } %>

And in my Controllers

[HttpPost]
public ActionResult Create(IList<TargetLang> targetLangs)
{
  if (ModelState.IsValid)
  {
    _repTargetsLangs.Add(targetLangs);
    _repTargetsLangs.Save();

    return RedirectToAction("Index");
  }

  return View(target);
}

The problem is that ModelState is always invalid, because you can submit only a name for all the languages - you haven't to translate the target in all languages -, but the name is mandatory for db, so the model Binder raises an error.

My question is: where I have to operate, to correct this error?
At the Model binder level?
In the Controller before the ModelState.IsValis call? And how?

I'm sure this case has happened to many, but I can't find an elegant, scalable solution.
Thanks.

A: 

Make name a hidden field that the model binder will find:

    <%: Html.Hidden("targetLangs[" + lang.id + "].idTarget", Model.id)%>
    <%= Html.Label("targetLangs_" + lang.id + "__name", "name")%>
    <%= Html.Hidden("targetLangs_" + lang.id + "__name", "name")%>
jfar
The Model Binder find the name with my syntax.
Sig. Tolleranza