2

I have created one page in MVC 3.0 Razor view. Create.cshtml

@model LiveTest.Business.Models.QuestionsModel
@*<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>*@
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <table cellpadding="1" cellspacing="1" border="0">
        <tr>
            <td>@Html.LabelFor(model => model.TestID)
            </td>
            <td>
                @Html.DropDownListFor(model => model.TestID, (IEnumerable<SelectListItem>)ViewBag.ItemIDList)@Html.ValidationMessageFor(model => model.TestID)
            </td>
        </tr>
        <tr>
            <td>@Html.LabelFor(model => model.Question)
            </td>
            <td>@Html.EditorFor(model => model.Question)@Html.ValidationMessageFor(model => model.Question)
                @Html.HiddenFor(model => model.QuestionsID)
            </td>
        </tr>
        <tr>
            <td>@Html.LabelFor(model => model.IsRequired)
            </td>
            <td>@Html.CheckBoxFor(model => model.IsRequired)@Html.ValidationMessageFor(model => model.IsRequired)
            </td>
        </tr>
        <tr>
            <td> 
            </td>
            <td>
                <input type="submit" value="Submit" />
            </td>
        </tr>
    </table>
}

QuestionsController.cs

 public class QuestionsController : Controller
    {
        #region "Attributes"
        private IQuestionsService _questionsService;
        #endregion

        #region "Constructors"
        public QuestionsController()
            : this(new QuestionsService())
        {
        }
        public QuestionsController(IQuestionsService interviewTestsService)
        {
            _questionsService = interviewTestsService;
        }
        #endregion
        #region "Action Methods"
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Create()
        {
            InterviewTestsService _interviewService = new InterviewTestsService();
            List<InterviewTestsModel> testlist = (List<InterviewTestsModel>)_interviewService.GetAll();
            ViewBag.ItemIDList = testlist.Select(i => new SelectListItem() { Value = i.TestID.ToString(), Text = i.Name });
            return View();
        }
        [HttpPost]
        public ActionResult Create(QuestionsModel questions)
        {
            if (ModelState.IsValid)
            {
                _questionsService.Add(questions);
                return RedirectToAction("Index");
            }
            InterviewTestsService _interviewService = new InterviewTestsService();
            List<InterviewTestsModel> testlist = (List<InterviewTestsModel>)_interviewService.GetAll();
            ViewBag.ItemIDList = testlist.Select(i => new SelectListItem() { Value = i.TestID.ToString(), Text = i.Name });
            return View(questions);
        }
        #endregion
    }

QuestionsModel.cs

public class QuestionsModel : IQuestionsModel
    {
        [ReadOnly(true)]
        public Guid QuestionsID { get; set; }

        [Required]
        [DisplayName("Question")]
        public string Question { get; set; }
        [DisplayName("Test ID")]
        public Guid TestID { get; set; }
        [DisplayName("Is Required")]
        public bool IsRequired { get; set; }
        [DisplayName("Created By")]
        public Guid CreatedBy { get; set; }
            }

Problem:

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

If I am adding the above two lines in Create.cshtml page and then I press submit button then it will fire validation message "Question is required!" if I am entering value in *Question field and then press submit button my [HttpPost]Create Method never execute.*

If I remove the above two lines from page then press submit button then it will execute [HttpPost]Create Method and fire validation from server side if I am entering value in Question field then also [HttpPost]Create executed.

Please help me.

rmtheis
  • 5,992
  • 12
  • 61
  • 78
imdadhusen
  • 2,493
  • 7
  • 40
  • 75

3 Answers3

0

I would check if any client side errors occurred when trying to submit the form. Check it from the browser console.

Also, make sure that you have completed your code with no validation errors before submitting the form.

tugberk
  • 57,477
  • 67
  • 243
  • 335
  • No form validate on client side work correctly but if i fill all required field and then press submit button then it dose not getting postback. Even i am not getting any validation error in browser – imdadhusen Sep 24 '11 at 03:39
0

Are you saying that the form doesn't validate client side and nothing ever get's POSTed back to your server?

Meaning, you click the submit button and nothing happens in the browser, correct?

The problem might be that your form isn't validating with the unobtrusive javascript library validation.

Only Bolivian Here
  • 35,719
  • 63
  • 161
  • 257
  • No form validate on client side work correctly but if i fill all required field and then press submit button then it dose not getting postback. Even i am not getting any validation error in browser – imdadhusen Sep 24 '11 at 03:39
0

The QuestionsModel class includes a property CreatedBy which is not included in your View.

Try either adding CreatedBy as a hidden field, or (better yet) remove CreatedBy from the QuestionsModel class, since it is not an attribute which should be exposed in the view.

I suspect that this missing property is the cause of the problem.

UPDATE

I ran some tests on your code, and it was not the CreatedBy property. Rather, your problem is that you are not supplying a QuestionsID value, but you included a hidden field for QuestionsID on the form.

Because QuestionsID is a value type, by default, the DataAnnotationsModelValidatorProvider adds a Required validator to the QuestionsID field. Because the field did not have a ValidationMessage, you could not see the validation error.

You can override the behavior of the default DataAnnotationsModelValidatorProvider by following the instructions in my answer here.

Community
  • 1
  • 1
counsellorben
  • 10,924
  • 3
  • 40
  • 38
  • no this is not cause of the problem because i have several other form which contain some field that i have not used in my view but still it is working. – imdadhusen Sep 24 '11 at 03:57