I am having a form in which i am doing client side validation but also doing server side validation.
my issue is in the form server side validation is working only for one field and if there is no error still the page is acquiring that much space which was allocation for validation summary
public class LandDetailController : Controller
{
// GET: LandDetail
public ActionResult Index()
{
CPSE_Landdetails cs = new CPSE_Landdetails();
cs.PsuList = GetPsu();
return View(cs);
}
[HttpPost]
public ActionResult Index(CPSE_Landdetails cs)
{
if (!string.IsNullOrEmpty(cs.LandFreeHold))
{
string landfree = @"^[0-9]\d{0,9}(\.\d{1,3})?%?$";
Regex re = new Regex(landfree);
if (!re.IsMatch(cs.LandFreeHold))
{
ModelState.AddModelError("LandFreeHold", "Land Entry Should Be Minimum uptp 2 Decimal Places( e.g. 10.02)");
}
}
if (!string.IsNullOrEmpty(cs.LandFreeHold_Mutated))
{
string landfree = @"^[0-9]\d{0,9}(\.\d{1,3})?%?$";
Regex re = new Regex(landfree);
if (!re.IsMatch(cs.LandFreeHold_Mutated))
{
ModelState.AddModelError("LandFreeHold_Mutated", "Land Free Hold Mutated Entry Should Be Minimum uptp 2 Decimal Places( e.g. 10.02)");
}
}
cs.PsuList = GetPsu();
return View(cs);
}
}
@model CPSEDetail.Models.CPSE_Landdetails
@{
/**/
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_LayoutPage1.cshtml";
}
<div class="container" style="margin-top:20px">
@if (!ViewData.ModelState.IsValid)
{
<span class="field-validation-error">@Html.ValidationMessage("LandFreeHold")</span><br />
<span class="field-validation-error">@Html.ValidationMessage("LandFreeHold_Mutated")</span><br />
}
@using (Html.BeginForm("Index", "LandDetail", FormMethod.Post))
{
<div class="card">
<div class="card-header">
<table width="1024">
<tr>
<td valign="bottom"><label>Choose PSU</label></td>
<td>@Html.DropDownListFor(model => model.CPSE_id, Model.PsuList, "-- Choose PSU --", new { @class = "form-control", required = "required" })</td>
</tr>
</table>
</div>
</div>
<div class="card" style="margin-top:20px">
<div class="card-header alert-danger"><label>Land Details</label></div>
<div class="card-body">
<table width="1024">
<tr>
<td valign="bottom"><label style="margin-left:30px">Land Free Hold</label></td>
<td>@Html.TextBoxFor(model => model.LandFreeHold, new { @class = "form-control", required = "required", @*@step = ".0001", @min = "0",*@type = "number", @step = "0.0001"})</td>
</tr>
<tr>
<td valign="bottom"><label>Land Free Hold Mutated</label></td>
<td>@Html.TextBoxFor(model => model.LandFreeHold_Mutated, new { @class = "form-control", required = "required", type = "number", @step = "0.0001" })</td>
</tr>
</table>
</div>
</div>
<div style="text-align:center;margin-top:20px">
<input type="submit" value="Submit Information" class="btn btn-primary" />
</div>
public partial class CPSE_Landdetails
{
public string CPSE_id { get; set; }
public string Land_Status { get; set; }
public string LandFreeHold { get; set; }
public string LandFreeHold_Mutated { get; set; }
public string LandFreeHold_NonMutated { get; set; }
public string LandFreeHole_Other { get; set; }
public string LandFreeHold_unit { get; set; }
public string LandLeaseHold { get; set; }
public string LandleaseHold_Remarks { get; set; }
public string LandTotal { get; set; }
public List<SelectListItem> PsuList { get; set; }
}