【问题标题】:Why a ModelState error being added more that once in my ASP.NET MVC site?为什么在我的 ASP.NET MVC 站点中添加了更多的 ModelState 错误?
【发布时间】:2010-11-16 18:24:43
【问题描述】:

我有一个相当复杂的模型,用于渲染表单并使用模型的元信息元信息执行验证。

视图模型有一个包含在表单中的子对象列表。子对象基于此:

[Table]
public class FieldInstance
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public long fiID { get; set; }
    [Column]
    public string fiLabel { get; set; }
    [Column]
    public bool fiIsRequired { get; set; }

    [DisplayName("alpha-numeric value")]
    [Column]
    public string fiStrValue { get; set; }

    [DisplayName("date/time value")]
    [Column]
    public DateTime? fiDateTimeValue { get; set; }

    [DisplayName("integer value")]
    [Column]
    public long? fiIntValue { get; set; }

    [DisplayName("decimal value")]
    [Column]
    public decimal? fiDecValue { get; set; }
    [Column]
    public int fiOrder { get; set; }
    [Column]
    public long fiStreamEntryID { get; set; }  // FK
    [Column]
    public long fiFieldTypeID { get; set; }    // FK

    // Relationship (many FieldInstances to one StreamEntry)
    // using EntityRef<StreamEntry> and ThisKey
    // which is "This" table's FK
    private EntityRef<StreamEntry> _StreamEntry;
    [System.Data.Linq.Mapping.Association(Storage = "_StreamEntry", ThisKey = "fiStreamEntryID")]
    public StreamEntry StreamEntry
    {
        get { return this._StreamEntry.Entity; }
        set { this._StreamEntry.Entity = value; }
    }

    // Relationship (one FieldInstance to one FieldType)
    // using EntityRef<FieldTypes> and ThisKey
    private EntityRef<FieldTypes> _FieldType;
    [System.Data.Linq.Mapping.Association(Storage = "_FieldType", ThisKey = "fiFieldTypeID")]
    public FieldTypes FieldTypes
    {
        get { return this._FieldType.Entity; }
        set { this._FieldType.Entity = value; }
    }

我正在使用Html.EditorFor() 语句来呈现列表中每个项目的局部视图模板。

如果我将文本放入 Datetime 字段,则 Html.ValidationSummary() 显示:

•The value 'asd' is not valid for date/time value.
•The value 'asd' is not valid for date/time value.

我的问题是错误被添加到ModelState 两次而不是一次。这是控制器动作:

    [HttpPost]
    public ActionResult EntryEdit(StreamEntry form)
    {
        // Get values
        StreamEntry entry = 
            form.seID == 0
            ? new StreamEntry()
            : genesisRepository.GetEntryByID(form.seID);

        // Get Stream for new entry
        if (form.seID == 0)
            entry.Stream = genesisRepository.GetStreamByID(form.StreamID);

        //Validate
        TryUpdateModel(entry);

        if (ModelState.IsValid)
            return RedirectToAction("EntryList", new { id = entry.StreamID });
        else
            return View(entry);
    }

为什么由于存在错误输入而触发的错误会导致两个错误实例被添加到 `ModelState 中?

【问题讨论】:

    标签: c# asp.net asp.net-mvc


    【解决方案1】:

    默认模型绑定器在尝试解析 StreamEntry form 操作参数时添加一次,当您在同一类型 (StreamEntry) 上调用 TryUpdateModel 方法时添加第二次。所以要么使用动作参数,要么使用TryUpdateModel 方法,但不要同时使用两者。就我个人而言,我总是使用动作参数,而不是 TryUpdateModel 方法。

    所以要修复您的错误:

    [HttpPost]
    public ActionResult EntryEdit(StreamEntry model)
    {
        if (!ModelState.IsValid)
        {
            // the model was not valid => redisplay the form so that 
            // the user can fix errors
            return View(model);
        }
        // Remark: might need to load the model's corresponding stream
        // from the repository if its values weren't posted
    
        // the model was valid => update it in the database
        genesisRepository.Update(model);
        return RedirectToAction("EntryList", new { id = model.StreamID });
    }
    

    【讨论】:

    • 感谢您提供如此详细的答复。我会玩这个。
    • 是的!感谢您的解决方案!
    猜你喜欢
    • 2018-06-21
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    相关资源
    最近更新 更多