我已經在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庫的核心?我只在這裏看到驗證插件,但沒有看到庫。 –