在我的应用程序中,我创建了一个方法来对数据进行排序,并创建一个列表,然后将其传递给我的数据层。我重载了该方法,使其接受params object[]和一个model。我正在编写一个接受model的重载方法,但是我在循环它时遇到了问题。
这是我的控制器方法
[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 的代码文件中循环模型?