小编get*_*tit的帖子

AutoMapper从源嵌套集合映射到另一个集合

编辑:标题不正确,我试图从源列表映射到嵌套模型的源列表.

我在尝试将列表映射到嵌套模型中列出的另一个列表时遇到问题.种类和不平整的种类.问题是我不知道如何做映射.

这是我的设置跟随我失败的映射尝试:

public class DestinationModel
{
    public DestinationNestedViewModel sestinationNestedViewModel { get; set; }
}

public class DestinationNestedViewModel
{
    public List<ItemModel> NestedList { get; set; }
}

public class SourceModel
{
    public List<Item> SourceList { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

其中Item和ItemModel已经在它们之间定义了映射

我不能这样做......

Mapper.CreateMap<SourceModel, DestinationModel>()
.ForMember(d => d.DestinationNestedViewModel.NestedList,
    opt => opt.MapFrom(src => src.SourceList))
Run Code Online (Sandbox Code Playgroud)

错误:

表达式'd => d.DestinationNestedViewModel.NestedList'必须解析为顶级成员.参数名称:lambdaExpression

然后我尝试了这样的事情:

.ForMember(d => d.DestinationNestedViewModel, 
 o => o.MapFrom(t => new DestinationNestedViewModel { NestedList = t.SourceList }))
Run Code Online (Sandbox Code Playgroud)

那里的问题是 NestedList = t.SourceList.它们分别包含不同的元素,ItemModelItem.所以,他们需要被映射.

我该如何映射?

c# model-view-controller nested viewmodel automapper

7
推荐指数
1
解决办法
2万
查看次数

ActionLinks的MVC动态路由值

我需要使用ActionLink链接到我的ViewModel A的编辑屏幕.

A有一个复合键,所以要链接到它,路由值必须有3个pramaters,如下所示:

<%: Html.ActionLink("EDIT", "Action", "Controller", 
    new { area = "Admin", Id1= 1, Id2= 2, Id3= 3 })%>
Run Code Online (Sandbox Code Playgroud)

如您所见,路由值包含控制器Action将接受的ID.

我希望能够从辅助函数生成路由值,如下所示:

public static Object GetRouteValuesForA(A objectA)
    {
        return new
        {
            long Id1= objectA.Id1,
            long Id2= objectA.Id2,
            long Id3= objectA.Id3
        };
    }
Run Code Online (Sandbox Code Playgroud)

然后在ActionLink助手中使用它,但我不知道如何将该结果传递给ActionHelper

objectA = new A(){Id1= objectA.Id1,Id2= objectA.Id2,Id3= objectA.Id3};
....
<%: Html.ActionLink("EDIT", "Action", "Controller", 
    new { area = "Admin", GetRouteValuesForA(objectA) })%>
Run Code Online (Sandbox Code Playgroud)

但是,这需要控制器操作接受匿名类型而不是3个属性的列表

我看到下面的链接合并匿名类型,但有没有其他方法来做到这一点? 合并匿名类型

c# anonymous-types routevalues asp.net-mvc-3

5
推荐指数
1
解决办法
1万
查看次数

访客模式可以用回调函数替换?

使用这两种技术有什么显着的好处吗?如果有变化,我的意思是访客模式:http://en.wikipedia.org/wiki/Visitor_pattern

以下是使用委托实现相同效果的示例(至少我认为它是相同的)

假设有一组嵌套元素:学校包含包含学生的部门

而不是使用访问者模式在每个集合项上执行某些操作,为什么不使用简单的回调(C#中的Action委托)

说这样的话

class Department
{
    List Students;
}

class School
{
    List Departments;

    VisitStudents(Action<Student> actionDelegate)
    {
        foreach(var dep in this.Departments)
        {
            foreach(var stu in dep.Students)
            {
                actionDelegate(stu);
            }
        }
    }
}

School A = new School();
...//populate collections

A.Visit((student)=> { ...Do Something with student... });
Run Code Online (Sandbox Code Playgroud)

*编辑示例,委托接受多个参数

假设我想通过学生和部门,我可以像这样修改动作定义:动作

class School
{
    List Departments;

    VisitStudents(Action<Student, Department> actionDelegate, Action<Department> d2)
    {
        foreach(var dep in this.Departments)
        {
            d2(dep); //This performs a different process.
            //Using Visitor pattern would avoid …
Run Code Online (Sandbox Code Playgroud)

c# delegates design-patterns visitor-pattern

5
推荐指数
1
解决办法
1347
查看次数

如何自动使用验证嵌套对象

假设有一个像这样class A的引用class B

class A
{
    [NotNull]
    string Alpha;
    B bObject;
}

class B
{
   [NotNull]
   string Beta;
}

A a = new A();
a.Alpha = "test"
a.bObject = new b();
a.bObject.Beta = null;
Run Code Online (Sandbox Code Playgroud)

现在,当为A的实例调用Create这样的东西时,代码会调用 NHibernate.Validator.Engine.ValidatorEngine.Validate

但是,它只验证了成员A (Alpha).当NHibernate尝试持久化对象时,它会失败,因为数据库不允许为nullObject.

是否可以对其进行配置,以便验证A还可以验证B?的属性?

*编辑:

我目前解决这个问题的方法是手动调用Validate(A.bObject)存储库需要创建/更新的任何地方A,例如:

在存储库中 A

public void Update(A entity)
{
    //I want to avoid having to do this in every class repository that has …
Run Code Online (Sandbox Code Playgroud)

.net c# nhibernate

5
推荐指数
1
解决办法
580
查看次数

MVC3嵌套的部分页面(和视图模型):如何从绑定表单字段?

我有2个这样的视图模型:

public class ViewModel1 // maps to Model1
{
    public string ViewModel1Desc { get; set; }
    public ViewModel2 ViewModel2 { get; set; }
    public ScheduleMasterEditViewModel()
    {
        ViewModel2= new ViewModel2();
    }
}

public class ViewModel2 // maps to Model2
{
    public string ViewModel2Desc { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在,我希望有一个ViewModel2的部分页面,并将其包含在ViewModel1的创建页面中:

Create.cshtml看起来像这样

@model ViewModels.ViewModel1
@using (Html.BeginForm()) {
    @Html.EditorFor(model => model.ViewModel1Desc )
    @Html.Partial("~/Views/ViewModel2/_ViewModel2Create.cshtml", Model.ViewModel2)
}
Run Code Online (Sandbox Code Playgroud)

_ViewModel2Create.cshtml看起来像

@model ViewModels.ViewModel2
@Html.EditorFor(model => model.ViewModel2Desc )
Run Code Online (Sandbox Code Playgroud)

问题是,在Model1的Create控制器上,没有任何内容绑定到ViewModel1.ViewModel2

我是以正确的方式做到这一点,还是应该写出所有这样的字段:

asp.net-mvc bind nested partials viewmodel

1
推荐指数
1
解决办法
1723
查看次数