【发布时间】:2015-01-07 02:25:34
【问题描述】:
您好,如果以这种方式选择,我正在尝试验证一个下拉菜单:
$('#dialog').dialog({
autoOpen: false,
width: 400,
resizable: false,
title: 'Add building',
modal: true,
open: function(event, ui) {
$(this).load("@Url.Action("AddView",new {@ViewBag.from})");
},
buttons: {
"Add": function () {
$("#LogOnForm").submit();
},
Cancel: function () {
$(this).dialog("close");
}
}
});
查看
@model View.ViewModel.AddBuildingViewModel
@{Html.EnableClientValidation();}
@using (Html.BeginForm("AddBuilding", "HolidaysEvents", FormMethod.Post, new { id = "LogOnForm" }))
{
@Html.HiddenFor(x => x.ReqestFrom, new { @Value = @ViewBag.from })
@Html.ValidationSummary(true)
<fieldset>
<legend>Building</legend>
<table>
<tr>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.Building.Name)
</div>
</td>
<td>
<div class="editor-field">
@Html.TextBoxFor(model => model.Building.Name)
@Html.ValidationMessageFor(model => model.Building.Name)
</div>
</td>
</tr>
<tr>
<td>
Country
</td>
<td>
<div class="editor-label">
@Html.DropDownListFor(model => model.Building.CountryId, new SelectList(Model.Countries, "Id", "Name"), "Choose Country... ", new { style = "height:35px"})
@Html.ValidationMessageFor(model => model.Building.CountryId)
</div>
</td>
</tr>
</table>
<p>
<input type="submit" value="Log On" style="display:none;" />
</p>
</fieldset>
}
型号
public class BuildingViewModel
{
public int Id { get; set; }
public string Name { get; set; }
[Required(ErrorMessage = "Required.")]
public int CountryId { get; set; }
}
代码
[HttpPost]
public ActionResult AddBuilding(AddBuildingViewModel buildingModel)
{
if (!ModelState.IsValid)
{
var modelError = new AddBuildingViewModel();
modelError.Countries = countryRepository.GetCountries().Select(x => new CountryViewModel { Id = x.Id, Name = x.Name }).ToList();
return PartialView("AddView", modelError);
}
var model = new HolidaysEventsViewModel();
buildingRepository.AddBuilding(buildingModel.Building.Name, buildingModel.Building.CountryId, WebSecurity.CurrentUserId);
model.Buildings = buildingRepository.GetBuildings(WebSecurity.CurrentUserId).Select(x => new BuildingViewModel { Id = x.Id, Name = x.Name }).ToList();
model.Countries = countryRepository.GetCountries().Select(x => new CountryViewModel {Id = x.Id, Name = x.Name}).ToList();
ViewBag.from = buildingModel.ReqestFrom;
return View("Index", model);
}
问题是当用户没有选择任何东西并且验证工作时对话框消失并且它只是带有验证消息的纯 html 页面,我怎样才能预防它并保持弹出窗口?
【问题讨论】:
标签: c# asp.net-mvc-4 jquery-ui-dialog