Created
September 17, 2015 13:59
-
-
Save arman-hpp/ddbe69113aa3ce6165b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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