0

I have used all the namespaces which are necessary for mvc validations in my model. My models and view code is as below i have tried all the possibilities and included all the javascript

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel;

namespace EAssessment.Models
{
    public class Question_Bank
    {
        [Display(Name="course")]
        [Required]
        public int Course_Id { get; set; }
        [Required]
        public int Standard_Id { get; set; }
        [Required]
        public int Subject_Id { get; set; }
        [Required]
        public int Lesson_Id { get; set; }
        [Required]
        public int Question_Type_Id { get; set; }
        [Required(ErrorMessage = "Required field")]
        public string Course_Name { get; set; }
        [Required(ErrorMessage = "Required field")]
        public string Standard_Name { get; set; }
        [Required(ErrorMessage = "Required field")]
        public string Subject_Name { get; set; }
        [Required(ErrorMessage = "Required field")]
        public string Lesson_Name { get; set; }
        [Required(ErrorMessage = "Required field")]
        public string Question_Type { get; set; }          

        public int Question_Id { get; set; }
        [Display(Name="course")]
        [Required()]
        public string Question{ get; set; }
        public int CntDynamicTextbox { get; set; }    

    }
}


<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> 

My Razor View markup

@using (Html.BeginForm())
{
     @Html.AntiForgeryToken()    
     @Html.ValidationSummary(true)
   <div style="height: auto; border: 1px solid #DBDBDB; width: 900px;">


        <br />
        <span>
            <label class="label"style="margin-left: 100px;">
                Course:
            </label >
        </span>
        <span>@Html.DropDownListFor(Model => Model.Course_Id, Courses, "Select Course", new { id = "ddlCourse", style = "margin-left:17px;height:25px;width:230px;", onchange = "loadStandard(this)" })</span>
           @Html.ValidationMessageFor(model => model.Course_Id)
      <span>
            <label  class="label" style="margin-left: 100px;">
                Standard:
            </label>
        </span>
        <span>@Html.DropDownListFor(Model => Model.Standard_Id, Standard, "Select Standard", new { id = "ddlStandard", style = "margin:0px 10px 0px 10px;height:25px;width:230px;", onchange = "loadSubjects(this)" })</span>
           @Html.ValidationMessageFor(model => model.Standard_Id)
        <br />
        <br />
        <br />

          <span>
            <label class="label" style="margin-left: 100px;">
                Subjects:
            </label>
        </span>

        <span>@Html.DropDownListFor(Model => Model.Subject_Id, Subject, "Select Subjects", new { id = "ddlSubjects", style = "margin:0px 10px 0px 10px;height:25px;width:230px;", onchange = "loadLessons(this)" })</span>
           @Html.ValidationMessageFor(model => model.Subject_Id)
        <span>
            <label class="label" style="margin-left: 90px;">
                Lesson:
            </label>
        </span>
        <span>@Html.DropDownListFor(Model => Model.Lesson_Id, Lesson, "Select Lesson", new { id = "ddlLesson", style = "margin:0px 0px 0px 25px;height:25px;width:230px;" })</span>
           @Html.ValidationMessageFor(model => model.Lesson_Id)
        <br />
        <br />
        <br />


       <span>
            <label class="label" style="margin-left: 56px;">
                Question Type:
            </label>
        </span>
        <span>@Html.DropDownListFor(Model => Model.Question_Type_Id, Question_Type, "Select Question Type", new { id = "ddlQuestion_Type", style = "margin:0px 290px 0px 13px;height:25px;width:230px;" })</span>
           @Html.ValidationMessageFor(model => model.Question_Type_Id)
        <br />
        <br />
        <br />

        <span>
            <label class="label" style="margin-left: 55px;vertical-align:top ! important">
                Enter Question:
            </label>
        </span>


       <span>
            @Html.TextAreaFor(Model => Model.Question, new { id = "txtQuestion", style = "width:450px;margin-left:10px;",onblur="valid();" })
               @Html.ValidationMessageFor(model => model.Question) 
             @*<input type="button" value="Add" onclick="add(document.forms[0].ddlQuestion_Type.value)" />where is your javascript?*@
            <a href="#"><img src="~/images/add-icon.png" onclick="add(document.forms[0].ddlQuestion_Type.value)"  /></a>
            <a href="#" style="font-family:'Segoe UI';font-size:medium;margin-left:-15px;color:#007FC5" onclick="add(document.forms[0].ddlQuestion_Type.value)" >Add Options</a>

             <a href="#" style="margin-left:10px"><img src="~/images/Remove.png" onclick="Remove_Options()"  /></a>
            <a href="#"  style="font-family:'Segoe UI';font-size:medium;color:#007FC5" onclick="Remove_Options()" >Delete Options</a>


       </span>


        <span id="fooBar" style="height: 50px; width: 80px"></span>
        <br />
       <br />
       <br />

    <center>
       <span style="margin-left:10px">
            <input type="submit" value="Save" class="btns" onclick="return CntDynamicTextbox()" />
            <input type="reset" value="Reset" class="btns" onclick="disablediv()" />
            <input type="hidden" name="cntdynamicTxtbox" id="cntdynamicTxtbox"  value=""/>
            <input type="hidden" name="flag" id="flag"  value=""/>
            <input type="hidden" name="RadioTxtboxvalue" id="RadioTxtboxvalue"  value=""/>
            <input type="hidden" name="checkboxTxtboxvalue" id="checkboxTxtboxvalue"  value=""/>

        </span>
        </center>
       <br />
       <br />
       <br />

</div>

}
5
  • try using ValidationSummary(false)
    – Lotok
    Jul 14, 2014 at 8:52
  • Is the form submitting the invalid model? Are you just not seeing the error messages? Is it both the summary and inline messages not showing? Do you have model declaration at the top of your page but omitted it from your example?
    – Lotok
    Jul 14, 2014 at 9:04
  • The Save button on click event calls CntDynamicTextbox(). Is this function doing anything unusual that might be preventing validation from being performed?
    – Dangerous
    Jul 14, 2014 at 9:09
  • no form is submitting the correct model and CntDynamicTextbox() is jst for counting no of textbox on form as it is generated dynamically and i dnt see any messages not inline and nor even summary form simply get posted with null values Jul 14, 2014 at 9:29
  • the script part into the class is a typo right?
    – VeNoMiS
    Jul 14, 2014 at 11:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.