action()和嵌套的路由值

本文关键字:路由 嵌套 action | 更新日期: 2023-09-27 18:05:46

我有两个类:

    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传递props。动作方法如下:

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

我怎样才能做我想做的?

action()和嵌套的路由值

不清楚你想要什么。如果你想把属性从类传递给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