可选属性中的MVC必需属性

时间:2013-12-09 23:01:28

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

我有两个实体:

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属性上收到验证错误,因为它是必需的。

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

2 个答案:

答案 0 :(得分:0)

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

我希望它有意义

利奥。

答案 1 :(得分:0)

根据目前的建议,这就是我现在所拥有的。

实体保持不变。

修改后的视图模型:

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方法中的null或空格测试解决了我在可选属性中的MVC必需属性的初始问题。它看起来怎么样?