【问题标题】:Not required fields in MVC4MVC4 中的非必填字段
【发布时间】:2013-07-08 17:49:04
【问题描述】:

我可以在我的剃刀视图中没有必填字段吗?

我有下面的视图,我希望隐藏字段不是必填字段。 目前它被视为强制性的,我的模型状态是错误的。

请多多指教?

@using P.M.O
@model O

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Create a New O</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.C) 
        @Html.TextBoxFor(model => model.C, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.Caption) 
    </div> <br />

    <div class="editor-label">
        @Html.LabelFor(model => model.N)
        @Html.TextBoxFor(model => model.N, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.N)
    </div> <br />

    <div class="editor-label">
        @Html.LabelFor(model => model.D)
        @Html.TextBoxFor(model => model.D, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.D)
    </div> 
    <br />
        @Html.HiddenFor(model=> model.P.Cr)
        <input type="submit" value="Create" />
</fieldset>
}  

型号:

 using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace P.M.O
{
[Serializable]
public partial class O
{
    /*** Construtor(s) ***/
    public O()
    {

    }

    public O(P obj)
        : this()
    {
        P= obj;
    }


    /*** Public Members ***/
    [Key, Display(Name = "Id")]
    public int PartyId { get; set; }


    /* IEntity */
    public string C{ get; set; }

    public string N{ get; set; }

    public string D{ get; set; }


    /* IAuditable */
    [NotMapped, ScaffoldColumn(false)]
    public System.DateTimeOffset Created
    {
        get { return P.C; }
        set { P.C= value; }
    }

    /* Navigation Properties */
    /// <summary>
    /// Foreign key to Party: PartyId
    /// Organization is subtype of  Party
    /// </summary>
    public virtual P P{ get; set; }

}
}

【问题讨论】:

  • 请也发布您的模型。
  • 发布了模型类..thanku
  • 我假设model.Party.Created 字段是DateTime 字段。如果您希望它不是必需的,则应将其声明为可为空的 DateTime 字段:public DateTime? Created { get; set; }

标签: asp.net-mvc-4 razor


【解决方案1】:

您应该将您的 Created 属性定义为可为空的 DateTimeOffset

/* IAuditable */
[NotMapped, ScaffoldColumn(false)]
public System.DateTimeOffset? Created
{
    get { return Party.Created; }
    set { Party.Created = value; }
}

编辑:并考虑到Party 属性可能是null

public System.DateTimeOffset? Created
{
    get 
    { 
        return Party == null ? null : Party.Created; 
    }
    set 
    { 
        if (Party == null) 
        {
            return; 
        } 
        else 
        { 
            Party.Created = value; 
        } 
    }
}

【讨论】:

  • 这实际上是数据库中的必填字段。我们对它有一个默认限制,我们也在工厂类中处理它。我的问题是组织是党的孩子。并且像 Created 这样的一些字段是从父级引用的(请参见上面的模型)。当我创建新组织时,我不会有它的关联方,因此我的组织模型在 Created 属性处以空引用失败。为了解决这个问题,我在视图中添加了隐藏字段,现在它被视为强制性的,如何解决这个问题?
  • 好吧,您的财产需要检查Party 是否为null。我添加了一个替代版本来进行这项检查。你能看看它是否有效吗?
  • get 属性中 和 DateTimeOffset 之间出现转换错误 :(
  • 上述属性的另一个问题是 - 我得到以下异常:PartyBiz.dll 中发生了“System.StackOverflowException”类型的未处理异常,在检查方得到此异常 == null
【解决方案2】:

使用自定义模型绑定器创建子对象或父对象。模型绑定器解决了这个问题。

【讨论】:

    猜你喜欢
    • 2014-03-13
    • 1970-01-01
    • 2012-05-23
    • 2013-04-18
    • 2019-08-23
    • 2020-03-29
    • 1970-01-01
    • 2019-07-09
    • 2013-08-05
    相关资源
    最近更新 更多