我正在使用基于ASP/ADO.NET的应用程序和数据结构,我正在将其中的一部分转换为ASP.NET MVC.在数据结构中,存在"可选的一对一"关系,其中两个表使用相同的主键和名称.基本上,这个表可以被认为是主表的"可选扩展".以下是该型号的样品:
public class ZoneMedia
{
public int ZoneMediaID { get; set; }
public string MediaName { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public virtual ZoneMediaText MediaText { get; set; }
}
public class ZoneMediaText
{
public int ZoneMediaID { get; set; }
public string Text { get; set; }
public int Color { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
显然,EF 4.1代码首先会自动映射这个问题.所以我意识到我必须明确指定映射.我试过这个:
modelBuilder.Entity<ZoneMedia>()
.HasOptional(zm => zm.ZoneMediaText);
modelBuilder.Entity<ZoneMediaText>()
.HasRequired(zmt => zmt.ZoneMedia)
.WithRequiredDependent(zm …Run Code Online (Sandbox Code Playgroud) 我想知道是否有一种优雅的方法可以将一组复杂类型添加到RouteValueDictionary或兼容类型?
例如,如果我有一个类和一个动作:
public class TestObject
{
public string Name { get; set; }
public int Count { get; set; }
public TestObject()
{
}
public TestObject(string name, int count)
{
this.Name = name;
this.Count = count;
}
}
public ActionResult Test(ICollection<TestObjects> t)
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
然后我知道如果我通过URL"/Test?t[0].Name=One&t[0].Count=1&t[1].Name=Two&t[1].Count=2"调用此动作,MVC将映射那些查询字符串参数自动返回ICollection类型.但是,如果我使用Url.Action()在某处手动创建链接,并且我想传递参数的RouteValueDictionary,当我向RouteValueDictionary添加ICollection时,Url.Action只是将其呈现为类型,如&t = System.Collections.Generic.List.
例如:
RouteValueDictionary routeValDict = new RouteValueDictionary();
List<TestObject> testObjects = new List<TestObject>();
testObjects.Add(new TestObject("One", 1));
testObjects.Add(new TestObject("Two", 2));
routeValDict.Add("t", testObjects);
// Does not properly create the parameters for the List<TestObject> collection.
string …Run Code Online (Sandbox Code Playgroud)