在我的应用程序中,我创建了一个方法来对我的数据进行排序,并创建一个传递给我的数据层的列表。我已经重载它以接受params对象[]和模型。我正在编写我的重载方法,它接受一个模型,但是我遇到了循环问题。
这是我的控制器方法
[HttpPost]
public ActionResult CreateUser(vw_UserManager_Model model)
{
// Return Model to view with error message when not valid.
if (!ModelState.IsValid == true)
{
return View(model);
}
else
{
List<string> myParams = DataCleaner.OrganizeParams(model);
}
这是我在将数据传递到数据层之前组织数据的方法
public static List<string> OrganizeParams(vw_UserManager_Model model)
{
List<string> myParams = new List<string>();
var modelProperties = model.GetType().GetProperties();
foreach (var property in model.GetType().GetProperties())
{
switch (property.PropertyType.Name)
{
case "String":
myParams.Add("System.String" + ":" + property.GetValue(property.PropertyType.Name, null));
break;
case "Guid":
myParams.Add("System.Guid" + ":" + property.GetValue(property.PropertyType.Name, null));
break;
case "Int32":
myParams.Add("System.Int32" + ":" + property.GetValue(property.PropertyType.Name, null));
break;
case "Boolean":
myParams.Add("System.Boolean" + ":" + property.GetValue(property.PropertyType.Name, null));
break;
}
}
return myParams;
}
我在Switch / Case逻辑中的操作实际上并不起作用,因为我在一个断点中查看了我的对象而无法看到我需要在代码中编写的内容。我知道我也可以使用IEnumerable,但我不太确定我该怎么做。
有什么建议吗?
摘要
如何在MVC3中的代码文件中循环模型?
答案 0 :(得分:0)
这对我有用,看起来你很近但是:)
public static List<string> OrganizeParams(vw_UserManager_Model model)
{
List<string> myParams = new List<string>();
foreach (var property in model.GetType().GetProperties())
{
switch (property.PropertyType.GenericTypeArguments.FirstOrDefault().Name.ToString())
{
case "String":
myParams.Add("System.String" + ":" + property.GetValue(model, null));
break;
case "Guid":
myParams.Add("System.Guid" + ":" + property.GetValue(model, null));
break;
case "Int32":
myParams.Add("System.Int32" + ":" + property.GetValue(model, null));
break;
case "Boolean":
myParams.Add("System.Boolean" + ":" + property.GetValue(model, null));
break;
}
}
return myParams;
}