的ValidationSummary方法显示在初始加载验证错误 [英] ValidationSummary method showing validation error on initial load

查看:344
本文介绍了的ValidationSummary方法显示在初始加载验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用的ValidationSummary(),我看到所需的误差名称初始创建。

我都试过初始化名称=在构造函数和不进行初始化(通过自带的名称= NULL ) - 同样的结果。

我怎样才能在这一领域的的ValidationSummary()不显示最初的错误?

模型

 公共类方案
{
    公众的Int32 ID {搞定;组; }
    //用户输入
    [需要]
    [显示名称(方案名称)]
    公共字符串名称{;组; }
    [需要]
    [显示名称(位置)
    公众的Int32 LocationId {搞定;组; }
    //..lots多,但省略了长度的问题

控制器

  // GET:方案/创建
    公众的ActionResult创建(方案的copyfrom = NULL)
    {
        VAR VM =新EditScenarioViewModel
        {
            场景=的copyfrom?新方案(User.Identity.Name)
        };
        VAR周期= _performancePeriods.GetPeformancePeriodsHistory()了ToList()。
        vm.scenario.FiscalPeriodStarting = periods.ElementAt(2).PerformancePeriodID; //默认为第3期回
        vm.scenario.FiscalPeriodEnding = periods.ElementAt(0).PerformancePeriodID;
        VM = prepareDropdowns(VM);
        返回查看(VM);
    }


解决方案

而不是传递参数使用的TempData的:

  copyMe.ID = 0; //重新设置ID
TempData的[CreateCopy] = copyMe
返回RedirectToAction(创建);

不带参数的Create():

 公众的ActionResult的Create()
{
  场景= TempData的[CreateCopy]作为场景;
  如果(方案== NULL)
  {
    场景=新方案(User.Identity.Name);
  }

When using ValidationSummary(), I am seeing a required error for Name on initial create.

I've tried both initializing Name="" in the constructor and not initializing (comes through as Name=null) - same result.

How can I get the ValidationSummary() to not display the initial error on this field?

Model

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

Controller

    // 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);
    }

解决方案

Instead of passing the parameter use TempData:

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

Create() with no parameters:

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

这篇关于的ValidationSummary方法显示在初始加载验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆