我已经在MVC 3.0 Razor视图中创建了一个页面。 Create.cshtml为什么我的[HttpPost]方法不会触发?
@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; }
}
问题:
<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>
如果我在添加上述两行Create.cshtml页面,然后我按下提交按钮,然后将火验证消息“是必需的问题!”如果我输入的值是* 问题字段,然后按提交按钮我的[HttpPost]Create
方法永远不会执行。 *
如果我删除从页上面的两行,然后按提交按钮,如果我在问题字段,则也[HttpPost]Create
执行输入值,那么它会执行从服务器端[HttpPost]Create
法和火验证。
请帮帮我。
我可能会脱节,但你是否包括jquery库的核心?我只在这里看到验证插件,但没有看到库。 –