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.
The
QuestionsModelclass includes a propertyCreatedBywhich is not included in your View.Try either adding
CreatedByas a hidden field, or (better yet) removeCreatedByfrom 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
CreatedByproperty. Rather, your problem is that you are not supplying aQuestionsIDvalue, but you included a hidden field for QuestionsID on the form.Because QuestionsID is a value type, by default, the
DataAnnotationsModelValidatorProvideradds 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.