Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to prevent a ModelBinder error in a mandatory fields of a list?
    primarykey
    data
    text
    <p>I've got this object created from EntityFramework from my database.</p> <pre><code>[EdmEntityTypeAttribute(NamespaceName="ContactCoreModel", Name="TargetLang")] [Serializable()] [DataContractAttribute(IsReference=true)] public partial class TargetLang : EntityObject { #region Factory Method /// &lt;summary&gt; /// Create a new TargetLang object. /// &lt;/summary&gt; /// &lt;param name="idTarget"&gt;Initial value of the idTarget property.&lt;/param&gt; /// &lt;param name="idLang"&gt;Initial value of the idLang property.&lt;/param&gt; /// &lt;param name="name"&gt;Initial value of the name property.&lt;/param&gt; 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 [...] /// &lt;summary&gt; /// No Metadata Documentation available. /// &lt;/summary&gt; [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(); } } } </code></pre> <p>In my Create View I'll do something like</p> <pre><code>&lt;% using (Html.BeginForm()) { %&gt; &lt;% foreach (var lang in Env.ActiveLangs) { %&gt; &lt;fieldset&gt; &lt;legend&gt;Data for language &lt;%: lang.name %&gt;&lt;/legend&gt; &lt;% var targetLang = Model.targetsLangs.FirstOrDefault(lt =&gt; lt.idLang.Equals(lang.id)) ?? new TargetLang(); %&gt; &lt;div class="editor-label"&gt; &lt;%: Html.Hidden("targetLangs.Index", lang.id)%&gt; &lt;%: Html.Hidden("targetLangs[" + lang.id + "].idLingua", lang.id)%&gt; &lt;%: Html.Hidden("targetLangs[" + lang.id + "].idTarget", Model.id)%&gt; &lt;%= Html.Label("targetLangs_" + lang.id + "__name", "name")%&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%: Html.TextBox("targetLangs[" + lang.id + "].name", targetLang.name)%&gt; &lt;%: Html.ValidationMessage("targetLangs[" + lang.id + "].name")%&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;% } %&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;% } %&gt; </code></pre> <p>And in my Controllers</p> <pre><code>[HttpPost] public ActionResult Create(IList&lt;TargetLang&gt; targetLangs) { if (ModelState.IsValid) { _repTargetsLangs.Add(targetLangs); _repTargetsLangs.Save(); return RedirectToAction("Index"); } return View(target); } </code></pre> <p>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.</p> <p>My question is: <em>where I have to operate, to correct this error?</em><br> At the Model binder level?<br> In the Controller before the ModelState.IsValis call? And how?</p> <p>I'm sure this case has happened to many, but I can't find an elegant, scalable solution.<br> Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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