2013-12-09 98 views
6

我有两个实体:可选属性中MVC必选属性

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属性上收到验证错误。

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

+0

另一种方法是删除视图模型中对实体类的依赖关系。你想使用数据传输对象(DTO)。这个http://stackoverflow.com/questions/5995140/models-viewmodels-dtos-in-mvc-3-application将帮助你开始。 – Jasen

回答

0

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

我希望它有道理

狮子座。

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或whitespace测试解决了可选属性中MVC所需属性的初始问题。它看起来如何?

+0

编辑一行时,不是生成一个新的“ParentThing”,而是想查找并检索ParentThing实例(通过id)。然后根据来自“ParentThingDTO”的新值编辑实体值。在创建时,您不会有现有的行,因此您可以安全地创建一个新的实体。 – Jasen