Hi v@run,
Check this example. Now please take its reference and correct your code.
Database
I have made use of the following table Users and UserActivation with the schema as follows.
You can download the database table SQL by clicking the download link below.
Download SQL file
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult Login(User user)
{
string userName = user.Username;
string password = user.Password;
UsersDBEntities entities = new UsersDBEntities();
User loginUser = entities.Users.Where(x => x.Username == userName && x.Password == password).FirstOrDefault();
if (loginUser != null)
{
UserActivation activation = new UserActivation();
activation.UserId = loginUser.UserId;
activation.ActivationCode = new Guid();
entities.UserActivations.AddObject(activation);
entities.SaveChanges();
return View("Success", user);
}
else
{
return View("Index", user);
}
}
public ActionResult Success()
{
return View();
}
}
View
Index
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<_Store_Data_In_Another_Table.User>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
</head>
<body>
<div>
<%using (Html.BeginForm("Login", "Home", FormMethod.Post))
{%>
<table>
<tr>
<td>Name</td>
<td><%:Html.TextBoxFor(m=>m.Username) %></td>
</tr>
<tr>
<td>Password</td>
<td><%:Html.PasswordFor(m => m.Password)%></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Login" />
</td>
</tr>
</table>
<% } %>
</div>
</body>
</html>
Success
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<_Store_Data_In_Another_Table.User>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Success</title>
</head>
<body>
<div>
Welcome <%=Model.Username %>
</div>
</body>
</html>
Screenshot
On Login button click if user details valid based on your database record the UserId will be inserted in the UserActivation table.