【问题标题】:MVC Required Property within Optional Property可选属性中的 MVC 必需属性
【发布时间】:2013-12-27 06:33:50
【问题描述】:

我有两个实体:

public class ParentThing
{
    [Key]
    public int Id { get; set; }

    [Required]
    public ChildThing TheFirstThing { get; set; }

    public ChildThing TheSecondThing { get; set; }
}

public class ChildThing
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string Code { get; set; }

    public string Name { get; set; }
}

还有一个视图模型:

public class ParentViewModel
{
    public string Message { get; set; }

    public ParentThing ParentThing { get; set; }
}

还有一个观点:

@using (@Html.BeginForm())
{
    <label>Code 1</label>
    @Html.EditorFor(model => model.ParentThing.TheFirstThing.Code)

    <label>Name 1</label>
    @Html.EditorFor(model => model.ParentThing.TheFirstThing.Name)

    <label>Code 2</label>
    @Html.EditorFor(model => model.ParentThing.TheSecondThing.Code)

    <label>Name 2</label>
    @Html.EditorFor(model => model.ParentThing.TheSecondThing.Name)

    <input type="submit" value="Save" />
}

在我的回帖中,我将 ParentThing 添加到上下文中并尝试保存更改。我收到有关 ParentThing 的 TheSecondThing 属性的 Code 属性的验证错误,因为它是必需的。

保存包含必需属性的可选属性有哪些替代方法?

【问题讨论】:

标签: c# asp.net-mvc entity-framework validation required


【解决方案1】:

正如 jamie 建议的那样,删除实体和模型之间的任何依赖关系...它们是两个独立的东西 不要在实体上使用数据注释,在模型上使用数据注释。删除模型的 ParentThing 属性,并根据需要在模型中添加原始属性(即 Message、ParentThingId、TheFirstThingId、TheFirstThingCode、TheFirstThingName 等),并将所有数据注释属性添加到模型中。如果您需要验证您的实体(您可能会)在您的业务逻辑上执行此操作。

我希望它有意义

狮子座。

【讨论】:

    【解决方案2】:

    作为对当前建议的回应,这就是我现在所拥有的。

    实体保持不变。

    修改后的视图模型:

    public class ParentViewModel
    {
        public string Message { get; set; }
    
        public string FirstThingCode { get; set; }
    
        public string FirstThingName { get; set; }
    
        public string SecondThingCode { get; set; }
    
        public string SecondThingName { get; set; }
    }
    

    修改后的视图:

    @using (@Html.BeginForm())
    {
        <label>Code 1</label>
        @Html.EditorFor(model => model.FirstThingCode)
    
        <label>Name 1</label>
        @Html.EditorFor(model => model.FirstThingName)
    
        <label>Code 2</label>
        @Html.EditorFor(model => model.SecondThingCode)
    
        <label>Name 2</label>
        @Html.EditorFor(model => model.SecondThingName)
    
        <input type="submit" value="Save" />
    }
    

    DTO:

    public class ParentThingDTO
    {
        public ParentThingDTO(ParentViewModel model)
        {
            FirstThingCode = model.FirstThingCode;
            FirstThingName = model.FirstThingName;
            SecondThingCode = model.SecondThingCode;
            SecondThingName = model.SecondThingName;
        }
    
        public int Id { get; set; }
    
        public string FirstThingCode { get; set; }
    
        public string FirstThingName { get; set; }
    
        public string SecondThingCode { get; set; }
    
        public string SecondThingName { get; set; }
    
        public ParentThing GenerateEntity()
        {
            var thing = new ParentThing();
            thing.TheFirstThing = new ChildThing
            {
                Code = FirstThingCode,
                Name = FirstThingName
            };
    
            if (!string.IsNullOrWhiteSpace(SecondThingCode))
            {
                thing.TheSecondThing = new ChildThing
                {
                    Code = SecondThingCode,
                    Name = SecondThingName
                };
            }
    
            return thing;
        }
    }
    

    控制器中的回发动作:

        [HttpPost]
        public ActionResult Create(ParentViewModel model)
        {
            try
            {
                var dto = new ParentThingDTO(model);
                var parentThing = dto.GenerateEntity();
    
                using (var context = new QuantumContext())
                {
                    context.ParentThings.Add(parentThing);
                    context.SaveChanges();
                    model.Message = "Saved";
                }
            }
            catch (Exception ex)
            {
                model.Message = ex.Message;
            }
    
            return View(model);
        }
    

    dto GenerateEntity 方法中的空值或空格测试解决了我最初的问题,即可选属性中的 MVC 必需属性。看起来怎么样?

    【讨论】:

    • 编辑行时,您不想生成新的ParentThing,而是要查找并检索ParentThing 实例(按ID)。然后根据来自ParentThingDTO 的新值编辑实体值。创建时您将没有现有行,因此可以安全地创建一个新实体。
    猜你喜欢
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多