Url.Action()和嵌套路由值

时间:2016-09-06 11:09:48

标签: c# asp.net-mvc

我有两个班级:

    public class Class1
{
    public int Id { get; set; }
    public string Param1 { get; set; }
    public string Param2 { get; set; }
    public Class2 Class2 { get; set; }
}
public class Class2
{
    public int Id { get; set; }
    public int Class1Id { get; set; }
    public string Param3 { get; set; }
    public string Param4 { get; set; }
    public Class1 Class1 { get; set; }
}

所以,我需要通过Url.Action方法传递道具:

Url.Action("Page",new {Class2.Param1 = "some text" })

我怎样才能做我想做的事?

1 个答案:

答案 0 :(得分:0)

目前还不清楚你想要什么。如果要将属性从类传递给Action,可以执行以下操作:

public class Class1 {
    public string Ok { get; set; }
}

public class Class2 {
    private Class1 cls;

    public int Test { get; set; }

    public string Ok {
        get { return cls.Ok; }
        set { cls.Ok = value; }
    }

    public Class2(Class1 cls) {
        this.cls = cls;
    }
}

var cls = new Class2(new Class1 {
    Ok = "Text"
});
cls.Test = 3;
var s = Url.Action("Page", "Controller", cls); // Result: /Controller/Page?Test=3&Ok=Text