0

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);
    }
pennstatephil
  • 1,593
  • 3
  • 22
  • 43
  • 1
    Are you aware that validation runs on any model passed into an action regardless of method (Get, Post, Put, etc)? And how are you going to determine what a `initial error` error is? – Erik Philips Jul 18 '14 at 20:36
  • The `ValidationSummary()` doesn't seem very useful if it's going to throw errors on a brand new object... what's the proper use pattern? – pennstatephil Jul 18 '14 at 20:38
  • With the just about infinite number of examples on the internet for asp.net-mvc methods of GETting the initial view with no errors, and a POSTed view with errors, I've having trouble understanding why you rolled them into a single action. – Erik Philips Jul 18 '14 at 20:40
  • they're separate actions... I have a `[HttpPost]` action too... they share the same view. The parameter is for creating a copy from an existing `Scenario`. – pennstatephil Jul 18 '14 at 20:45
  • Then why does the GET have a parameter? And please post [the complete code example](http://stackoverflow.com/help/mcve). – Erik Philips Jul 18 '14 at 20:47
  • `ValidationSummary()` works as expected on the post. I explained the parameter. Please explain what more information you need. – pennstatephil Jul 18 '14 at 20:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57582/discussion-between-erik-philips-and-pennstatephil). – Erik Philips Jul 18 '14 at 20:48

1 Answers1

1

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); 
  }
Erik Philips
  • 53,428
  • 11
  • 128
  • 150