我有一个ModelState.AddModelError,当Month值(在Viewmodel中指定)等于de DB时,会添加一个错误。但是问题是,当使用模式引导程序时,它不起作用。模态转到唯一的页面,而不是停留在模态视图中
这是我的控制器:
var monthexist = _context.TBL_SalesObjective.Where(x => x.SalesObjectiveID == model.SalesObjectiveID && x.UserID == model.UserID).FirstOrDefault().Month; //verifica o mes no userID e selesid, se existe o mês escolhido
if (monthexist != model.Month)
{
TBL_SalesObjective item2 = new TBL_SalesObjective();
item2.Month = (DateTime)model.Month;
item2.UserID = (int)model.UserID;
item2.SalesObjectiveMonth = (decimal)model.SalesObjectiveMonth;
item2.GrossMargin = (decimal)model.GrossMargin;
item2.ConversionRate = (decimal)model.ConversionRate;
item2.CreateDate = DateTime.Today;
item2.CreateBy = userID;
_context.TBL_SalesObjective.Add(item2);
_context.SaveChanges();
return RedirectToAction("Index");
}
else
{
ModelState.AddModelError("Month", "Error already exist");
return View(model);
}
这是我的看法以及本月的模式:
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MMM/yyyy}")]
[Display(Name = "Select Month")]
public Nullable<System.DateTime> Month { get; set; }
<div class="form-group">
@Html.LabelFor(m => m.Month, new { @class = "control-label col-sm-3" })
<div class="col-sm-9 input-group">
<input type="text" id="monthPicker" name="Month" placeholder="CheckIn" readonly>
@Html.ValidationMessageFor(model => model.Month, "", new { @class = "text-danger " })
</div>
</div>
当我按保存时,视图转到此处: Modal bootstrap error