Click here to Skip to main content
15,888,257 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm using mvc5 for my applicatio in this form validation is successfully done and datavalidation will be checked but for invalid data error message is not displayed.

Quote:
my view is like below

@using (Html.BeginForm("TimeSheet", "TimeSheet", FormMethod.Post))
{
    @*@Html.AntiForgeryToken()*@
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="row">
        <div class="col-sm-3">
            Select Date: <input type="text" id="Date" Name="Date" class="form-control" placeholder="Select Date">
            @Html.ValidationMessageFor(model => model.Date)
            @*<span class="field-validation-valid text-danger"
                      data-valmsg-for="StudentName" data-valmsg-replace="true">
                    The Date field is required.
                </span>*@
        </div>
        <div class=" col-sm-3">
            Select Employee:
            @Html.DropDownListFor(m => m.FullName, Model.EmployeeNamesList, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.FullName)
        </div>
    <div class="row">
        <div class="col-sm-3">
            Select InTime:<input type="text" id="Intime" Name="Intime" class="form-control" placeholder="Select InTime">
            @Html.ValidationMessageFor(model => model.Intime)
        </div>
        <div class="col-sm-3">
            Select LunchOut Time:<input type="text" id="LunchOutTime" Name="LunchOutTime" class="form-control" placeholder="Select LunchOut Time">
            @Html.ValidationMessageFor(model => model.LunchOutTime)
        </div>
        <div class="col-sm-3">
            Select LunchIn Time:<input type="text" id="LunchInTime" Name="LunchInTime" class="form-control" placeholder="Select LunchIn Time">
            @Html.ValidationMessageFor(model => model.LunchInTime)
        </div>
        <div class="col-sm-3">
            Select OutTime:<input type="text" id="OutTime" Name="OutTime" class="form-control" placeholder="Select OutTime">
            @Html.ValidationMessageFor(model => model.OutTime)
        </div>
    </div>
    <br />
    <div class="row text-center">
        <input type="submit" value="Submit" style="align-items:center" id="viewdetails" class="btn btn-lg btn-primary" />
    </div>
}
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}


What I have tried:

my model is:
public class TimeSheet
    {
        public int ID { get; set; }
        public int EmpID { get; set; }
        //[Required]
        [Required]
        [Display(Name = "Select user type:")]
        public DateTime Date { get; set; }
        [Required]
        [Display(Name = "Select user type:")]
        public TimeSpan Intime { get; set; }
        [Required]
        [Display(Name = "Select user type:")]
        public TimeSpan LunchOutTime { get; set; }
        [Required]
        [Display(Name = "Select user type:")]
        public TimeSpan LunchInTime { get; set; }
        [Required]
        [Display(Name = "Select user type:")]
        public TimeSpan OutTime { get; set; }
        [Required]
        public string FullName { get; set; }
        public List<System.Web.Mvc.SelectListItem> EmployeeNamesList { get; set; }

    }

and my controller code is:
[HttpGet]
        public ActionResult TimeSheet()
        {
            using (SnovaHubEntities entities = new SnovaHubEntities())
            {
                Models.TimeSheet model = new Models.TimeSheet();
                model.EmployeeNamesList = GetEmployeenames(); 
                return View(model);
            }
        }
        public ActionResult ViewTimeSheet()
        {
            return View();
        }
        [HttpPost]
        public ActionResult TimeSheet(Models.TimeSheet model)
        {
            if (ModelState.IsValid) 
            {
                // my Code 
                        
            }
            return RedirectToAction("TimeSheet", "TimeSheet");
}


i don't know what is the error in my code
Quote:
and i don't know about ajax so could you please help to solve this
Posted
Comments
Richard Deeming 13-Feb-18 9:26am    
You should be getting client-side validation errors. If you're not, then check your browser's error console for errors.

As for the server-side validation errors, you won't see them if you redirect. You would typically want something like:
[HttpPost]
public ActionResult TimeSheet(Models.TimeSheel model)
{
    if (!ModelState.IsValid)
    {
        // Validation failed; re-display the view with the validation errors:
        return View(model);
    }
    
    ...
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900