After I run, when I type the value in Username textbox and clicked submit button.
In Controller I check the object its showing null values…. If i remove the forloop and list its working…… give me a solution
Controller :
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(LogonViewModel lvm)
{
LogonViewModel lv = new LogonViewModel();
return View();
}
Model:
public class LogonViewModel
{
[Required(ErrorMessage = "User Name is Required")]
public string UserName { get; set; }
}
View
@model IList<clientval.Models.LogonViewModel>
@{
ViewBag.Title = "Index";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="../../assets/js/val.js" type="text/javascript"></script>
@using (Html.BeginForm())
{
for (int i = 0; i < 1; i++)
{
@Html.LabelFor(m => m[i].UserName)
@Html.TextBoxFor(m => m[i].UserName)
@Html.ValidationMessageFor(per => per[i].UserName)
<input type="submit" value="submit" />
}
}
Your POST controller action must take a collection because that’s what you have in your view: