https://dotnetfiddle.net/AyeU9M
Если вы добавите точку останова в действие Post, вы увидите значения в действии, возвращенные из View
Таблица
CREATE TABLE [AHCCCS\KXBlau].[UserLog](
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserName] [varchar](30) NULL,
[EndPoint] [varchar](30) NULL,
[Date] [datetime] NULL,
CONSTRAINT [PK_UserLog] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Контроллер / Просмотр модели
public class UserLogViewModel
{
public string UserName { get; set; }
public string EndPoint { get; set; }
public string Date { get; set; }
}
public class HomeController : Controller
{
private readonly SMARTEntities3 _db = new SMARTEntities3();
[HttpPost]
public ActionResult MyIndex([Bind(Include = "UserName, EndPoint, Date")] UserLogViewModel log)
{
ViewBag.UserName = HttpContext.User.Identity.Name;
if (ModelState.IsValid)
{
//from viewmodel to object
UserLog userLog = new UserLog { Date = DateTime.Parse(log.Date), EndPoint = log.EndPoint,
UserName = log.UserName };
_db.UserLogs.Add(userLog);
_db.Entry(log).State = EntityState.Added;
_db.SaveChanges();
}
return View();
}
public ActionResult Tut142()
{
return View();
}
View
@model Testy20161006.Controllers.UserLogViewModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Tut142</title>
</head>
<body>
@using (Html.BeginForm("MyIndex", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<br />
<br />
<p>@User.Identity.Name</p>
<p>
@*@Html.TextBoxFor(model => model.Date, new { @Value = @DateTime.Now.ToShortDateString() })*@
@Html.LabelFor(model => model.UserName)
@Html.TextBoxFor(model => model.UserName)
</p>
<p>
@Html.LabelFor(model => model.EndPoint)
@Html.TextBoxFor(model => model.EndPoint)
</p>
<p>
@Html.LabelFor(model => model.Date)
@Html.TextBoxFor(model => model.Date)
</p>
<input type="submit" value="Go" />
}
</body>
</html>