Jquery LIstBoxFor未正确验证MVC
我正在尝试使用HTML.ListboxFor()控件对页面进行验证。我的页面上有几个HTML.DropDownListFor()控件,可以按设计进行验证。但是,每当我在列表框中选择一个项目时,页面都会在控制器上触发HttpPost操作方法,绕过将触发验证的form.submit()函数 如果需要任何其他信息来解决此问题,请告诉我 我的HTML如下Jquery LIstBoxFor未正确验证MVC,jquery,validation,model-view-controller,Jquery,Validation,Model View Controller,我正在尝试使用HTML.ListboxFor()控件对页面进行验证。我的页面上有几个HTML.DropDownListFor()控件,可以按设计进行验证。但是,每当我在列表框中选择一个项目时,页面都会在控制器上触发HttpPost操作方法,绕过将触发验证的form.submit()函数 如果需要任何其他信息来解决此问题,请告诉我 我的HTML如下 //....more HTML <div class="span6">
//....more HTML
<div class="span6">
@Html.LabelFor(m => m.RaceAsList)
<div class="controls">
@Html.ListBoxFor(m => m.RaceAsList, Race.SelectRace, Html.GetI18LHtmlAttributesFor(m => m.Race, new { @class = "span4 subtext upper defaultIsZero hasPatientDeclines", tabindex = 13 }))
@Html.ValidationMessageFor(m => m.RaceAsList)
<br />
<span class="muted">@(Html.Raw(Kiosk.ResourceFiles.Resource.RaceHelp))</span>
</div>
</div>
<div class="span6">
@Html.LabelFor(m => m.Ethnicities)
<div class="controls">
@Html.DropDownListFor(m => m.Ethnicities, Ethnicities.SelectEthnicities, Html.GetI18LHtmlAttributesFor(m => m.Ethnicities, new { @class = "span4 upper defaultIsZero hasPatientDeclines", tabindex = 14 }))
@Html.ValidationMessageFor(m => m.Ethnicities)
</div>
</div>
//....more HTML
我的控制器如下:
//...more javascript
$("form").submit(function() {
if (preSubmit != undefined && $.isFunction(preSubmit)) {
preSubmit();
}
if (!isForeignAddress) {
$(".phone").each(function() {
nums = pv.stripAlphaChars($(this).val());
$(this).val(nums);
});
}
if ($(".requireOptOut").size() > 0) {
requireOptOut($(".requireOptOut:first"));
}
return true;
});
//...more javascript
//...more C#
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult PatientInfo(KioskPatientEntity model, FormCollection form)
{
LoadFromSession();
var kioskPatient = PatientManagerService.GetPatientBySession(NavigationSession.SessionPk, Environment, KioskPatientChildPrefetch.None);
if (kioskPatient != null)
{
kioskPatient.MergeChanges(model);
}
else
{
kioskPatient = model;
kioskPatient.SessionPk = NavigationSession.SessionPk;
}
var properties = new []
{
KioskPatientFieldIndex.FirstName.ToString()
,KioskPatientFieldIndex.MiddleName.ToString()
,KioskPatientFieldIndex.LastName.ToString()
,KioskPatientFieldIndex.Gender.ToString()
,KioskPatientFieldIndex.Ssn.ToString()
,KioskPatientFieldIndex.SsnOptedOut.ToString()
,KioskPatientFieldIndex.BirthDate.ToString()
,KioskPatientFieldIndex.Address1.ToString()
,KioskPatientFieldIndex.Address2.ToString()
,KioskPatientFieldIndex.Zip.ToString()
,KioskPatientFieldIndex.City.ToString()
,KioskPatientFieldIndex.State.ToString()
,KioskPatientFieldIndex.Country.ToString()
,KioskPatientFieldIndex.Race.ToString()
,KioskPatientFieldIndex.Ethnicities.ToString()
,KioskPatientFieldIndex.PreferredLanguage.ToString()
,KioskPatientFieldIndex.HearFrom.ToString()
,KioskPatientFieldIndex.Practice.ToString()
,KioskPatientFieldIndex.AlternateLocation.ToString()
};
kioskPatient.InitializeRaceList(ListManagerService.GetRaceList());
//var List = kioskPatient.RaceAsList;
//var raceComingtrhough = kioskPatient.Race;
//var nameComing = kioskPatient.FirstName;
return SaveKioskPatientEntity(kioskPatient, properties,
new SequenceButtonTypes[] {SequenceButtonTypes.NextButton});
}
//...more C#
默认情况下,仅从列表框中选择一个项目不会触发POST提交。您必须运行一些其他javascript来执行更改提交。此外,即使事情无效,您发布的JS也不会停止表单提交,您永远不会
返回false
或有e.preventDefault()
在应用程序中,有一个按钮触发表单提交。如果没有选择列表框,则整个页面验证将在单击按钮时提交表单。如果列表框中有选择项,则不会激发form.submit()函数,而是激发HttpPost PatientInfo()。我明白了。。在这种情况下,您的Javascript中一定有错误。打开浏览器控制台,检查submitI上是否有任何错误。我已经这样做了,并且没有js错误。表单.submit()永远不会被命中,相反,HttpPost PatientInfo()会被触发,但只有在列表框中有选择时才会触发。我已将该控件更改为HTML.DropDownListFor()控件以进行故障排除,页面功能与设计相同,只是没有使用列表框。是否确实没有调用form.submt()
函数?您当前的javascriptform.submt()
侦听器不会停止表单提交,因为您没有任何返回false代码>在那里。尝试添加console.log('hello')
到您的表单。submit()
以确保它正在启动