Skip to content

Instantly share code, notes, and snippets.

@arman-hpp
Created September 17, 2015 13:59
Show Gist options
  • Save arman-hpp/ddbe69113aa3ce6165b9 to your computer and use it in GitHub Desktop.
Save arman-hpp/ddbe69113aa3ce6165b9 to your computer and use it in GitHub Desktop.
public class HomeController : Controller
{
public ActionResult Index()
{
return View("Home");
}
public ActionResult Login(string txtUsername, string txtPassword)
{
var x = Request.ToObj<User>();
return View();
}
}
public static class Ex
{
public static T ToObj<T>(this HttpRequestBase httpRequestBase) where T : new()
{
var newObj = new T();
var typeOfObj = typeof (T);
var props = typeOfObj.GetProperties();
foreach (var prop in props)
{
if (prop.PropertyType == typeof (string) && httpRequestBase.Form.AllKeys.Any(k => k == prop.Name))
prop.SetValue(newObj, httpRequestBase.Form[httpRequestBase.Form.AllKeys.FirstOrDefault(k => k == prop.Name)]);
}
return newObj;
}
}
public class User
{
public string Username { get; set; }
public string Password { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment