1

I have used range validation but this is not working. I am adding model,controller and view code.Please help(i have added only related fields only in this code).

Model is :

public class TicketDetailModel : TicketModelBase
    {
        public WorkOnTicketCreateModel WorkOnTicketCreateModel { get; set; }
    }
public class TicketModelBase
    {
        [Required]
        [Range(1, int.MaxValue, ErrorMessage = "Please enter a value bigger than {0}")]
        public int EstimatedTime { get; set; }
        public virtual List<WorkOnTicket> WorkOnTickets { get; set; }
    }
public class WorkOnTicketCreateModel : WorkOnTicketModelBase
    {
        [Required]
        [Display(Name = "AssignedToUser")]
        public int AssignedToUserId { get; set; }
        public IEnumerable<SelectListItem> AssignedUser { get; set; }

        [Required]
        [Display(Name = "Ticket Status")]
        public int TicketStatusId { get; set; }

        public TicketStatus TicketStatusVal { get; set; }

        public IEnumerable<SelectListItem> TicketStatus { get; set; }

    } 
public class WorkOnTicketModelBase
    {
        public int Id { get; set; }

        [Required]
        public int EstimateHours { get; set; }

        [Required]
        [Range(1, int.MaxValue, ErrorMessage = "Please enter a value bigger than {0}")]
        public int WorkedHours { get; set; }

    }
Contoller:
        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Details(TicketDetailModel model, IEnumerable<HttpPostedFileBase> file)
        {
            using (ITransaction transaction = this.nhSession.BeginTransaction())
            {

                var ticketObj = this.nhSession.QueryOver<Ticket>().Where(t => t.Id == model.Id).SingleOrDefault();
                var workOnTicket = new WorkOnTicket();

             workOnTicket.Ticket = ticketObj;
                    workOnTicket.WorkedHours = model.WorkOnTicketCreateModel.WorkedHours;
                    workOnTicket.EstimateHours = model.WorkOnTicketCreateModel.EstimateHours;               
                ticketObj.WorkOnTickets.Add(workOnTicket);
                this.nhSession.Save(ticketObj);
                transaction.Commit();
            }
            return RedirectToAction("Details", new { id = model.Id, milestoneId = model.Milestone.Id, projectId = model.Project.Id });
        }

View:-

@model AnkTech.TicketManagement.Web.Models.Ticket.TicketDetailModel
@using (Html.BeginForm("Details", "Ticket", FormMethod.Post, new { enctype = "multipart/form-data" }))
{   
@Html.ValidationSummary(true)
 @Html.TextBoxFor(model => model.WorkOnTicketCreateModel.EstimateHours, new { @id = "work_remaining", @class = "s-mini", size = "2" })
Worked hours:                                                                            @Html.TextBoxFor(model => model.WorkOnTicketCreateModel.WorkedHours, new { @id = "worked_hours", @class = "s-mini", size = "2" })
<input type="submit" value="Submit" tabindex="2" name="commit" id="submit-comment"
 class="gray-btn">
}

I have deleted all rmaining fields. i have added only fields to which related i am asking, please help.

Hemant Soni
  • 2,051
  • 4
  • 16
  • 16

1 Answers1

3

You need to use ModelState.IsValid to check that the model is actually valid. Currently you assign validation attributes but never check them:

[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Details(TicketDetailModel model, IEnumerable file) {
    if (!ModelState.IsValid)
    {
        // Handle error
    }
    else
    {    
        using (ITransaction transaction = this.nhSession.BeginTransaction()) {
            var ticketObj = this.nhSession.QueryOver<Ticket>().Where(t => t.Id == model.Id).SingleOrDefault();
            var workOnTicket = new WorkOnTicket();
            workOnTicket.Ticket = ticketObj;
            workOnTicket.WorkedHours = model.WorkOnTicketCreateModel.WorkedHours;
            workOnTicket.EstimateHours = model.WorkOnTicketCreateModel.EstimateHours;               
            ticketObj.WorkOnTickets.Add(workOnTicket);
            this.nhSession.Save(ticketObj);
            transaction.Commit();
        }            
    }
    return RedirectToAction("Details", new { id = model.Id, milestoneId = model.Milestone.Id, projectId = model.Project.Id });
}
Ant P
  • 24,820
  • 5
  • 68
  • 105
  • Yes i can manage it by modelstate.isvalid but it will check after post back, while i want, when we click it should display error meessage front of that fields. – Hemant Soni Apr 18 '13 at 10:44
  • Do you have unobtrusive validation enabled? Is it working elsewhere? There are reports of problems with numerical validation between jQuery Validate and MS Unobtrusive Validation in certain versions. This may help you: http://stackoverflow.com/questions/14889431/client-side-validation-trips-on-dataannotation-range-attribute – Ant P Apr 18 '13 at 10:49
  • Also make sure that you *do* perform server-side validation as client-side validation alone is not robust. – Ant P Apr 18 '13 at 10:49
  • as soon as i press submit button error message should be display if we have entered 0 – Hemant Soni Apr 18 '13 at 10:51
  • i have not used jquery validation, exactly same thing is working on other page, but not working on this page, i can not understand why? – Hemant Soni Apr 18 '13 at 10:59
  • Thanks for reply, now i have solved this problem now. I have missing something, i am sharing this, that's why this can help to other. I have missed @Scripts.Render("~/bundles/jqueryval") like as @section scripts{@Scripts.Render("~/bundles/jqueryval")} section – Hemant Soni Apr 18 '13 at 12:13