ValidationSummary方法显示初始加载时的验证错误

时间:2014-07-18 20:34:09

标签: c# validation asp.net-mvc-4 validationsummary

使用ValidationSummary()时,我在初始创建时看到Name所需的错误。

我已经尝试在构造函数中初始化Name=""而不是初始化(来自Name=null) - 同样的结果。

如何让ValidationSummary()不显示此字段的初始错误?

模型

public class Scenario
{
    public Int32 ID { get; set; }
    //user input
    [Required]
    [DisplayName("Scenario Name")]
    public String Name { get; set; }
    [Required]
    [DisplayName("Location")]
    public Int32 LocationId { get; set; }
    //..lots more, but omitted for question length

控制器

    // GET: Scenario/Create
    public ActionResult Create(Scenario copyFrom = null)
    {
        var vm = new EditScenarioViewModel
        {
            scenario = copyFrom ?? new Scenario(User.Identity.Name),
        };
        var periods = _performancePeriods.GetPeformancePeriodsHistory().ToList();
        vm.scenario.FiscalPeriodStarting = periods.ElementAt(2).PerformancePeriodID; //default is 3rd period back
        vm.scenario.FiscalPeriodEnding = periods.ElementAt(0).PerformancePeriodID;
        vm = PrepareDropdowns(vm);
        return View(vm);
    }

1 个答案:

答案 0 :(得分:1)

而不是传递参数使用TempData:

copyMe.ID = 0; //reset ID 
TempData["CreateCopy"] = copyMe 
return RedirectToAction("Create"); 

没有参数的Create():

public ActionResult Create() 
{ 
  scenario = TempData["CreateCopy"] as Scenario; 
  if (scenario == null) 
  { 
    scenario = new Scenario(User.Identity.Name); 
  }