简体   繁体   English

如何防止列表的必填字段中出现ModelBinder错误?

[英]How to prevent a ModelBinder error in a mandatory fields of a list?

I've got this object created from EntityFramework from my database. 我已经从数据库的EntityFramework创建了这个对象。

[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. 问题在于ModelState始终是无效的,因为您只能为所有语言提交一个名称-尚未以所有语言翻译目标-但该名称对于db是必需的,因此Model Binder会引发错误。

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? 在Controller之前调用ModelState.IsValis? And how? 如何?

I'm sure this case has happened to many, but I can't find an elegant, scalable solution. 我敢肯定这种情况发生在很多人身上,但是我找不到一个优雅的,可扩展的解决方案。
Thanks. 谢谢。

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")%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM