English 中文(简体)
为什么在我的伙伴关系中再次出现模拟国家错误。 NET MVC站点
原标题:Why a ModelState error being added more that once in my ASP.NET MVC site?

我有一个相当复杂的模式,我利用模型扫描信息提供表格并进行验证。

《观点模式》有一份以表格形式列出的儿童物品清单。 objects

[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 field,则Html.ValidationSummary(:

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

我的问题是,正把错误加在<条码>中。 这是控制者的行动:

    [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);
    }

为什么由于输入不正确而引起错误,导致“示范国家”出现两个错误。

最佳回答

缺省模式一旦试图使<代码>上下层,即加起来。 缩略语 TryUpdateModel 同一类型(StreamEntry/code>)。 因此,要么使用行动论据,要么使用<代码>,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 });
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签