Robię bardzo prostą aplikację, która wprowadza dane do bazy:
To jest widok:
<body>
<div>
<table>
<tr>
<td>
First Name:
</td>
<td>
<input type="text" id="TxtFName" name="FirstName" value="" />
</td>
</tr>
<tr>
<td colspan="2" align="right">
@Html.ValidationMessage("FirstName")
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<input type="text" id="TxtLName" name="LastName" value="" />
</td>
</tr>
<tr>
<td>
Salary:
</td>
<td>
<input type="text" id="TxtSalary" name="Salary" value="" />
</td>
</tr>
<tr>
<td colspan="2">
<form action="/Employee/SaveEmployee" method="post">
<input type="submit" name="BtnSubmit" value="Save Employee" />
<input type="submit" name="BtnSubmit" value="Cancel" />
<input type="button" name="BtnReset" value="Reset" onclick="ResetForm();" />
</form>
</td>
</tr>
</table>
</div>
</body>
To metoda kontrolera po naciśnięciu submita:
public ActionResult SaveEmployee(Employee e, string BtnSubmit)
{
switch (BtnSubmit)
{
case "Save Employee":
if (ModelState.IsValid)
{
EmployeeBusinessLayer empBal = new EmployeeBusinessLayer();
empBal.SaveEmployee(e);
return RedirectToAction("Index");
}
else
return View("CreateEmployee");
case "Cancel":
return RedirectToAction("Index");
}
return new EmptyResult();
}
A to jest model:
public class Employee
{
[Key]
public int EmployeeId { get; set; }
[Required]
public string FirstName { get; set; }
public string LastName { get; set; }
public int Salary { get; set; }
}
Dlaczego gdy wpisuje dane w textboxy i daje Save Employee to ModelState.IsValid jest false?