1

I followed the routine suggested in SO the one with two buttons with the same name.

I have two submit buttons in my View.

 <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="SaveAndAddMore" name="command" class="btn btn-default" />
            <input type="submit" value="SaveAndContinue" name="command" class="btn" />

        </div>
    </div>

In the controller, I am checking (or atleast want to check) the value of command passed

     [HttpPost]
     [ValidateAntiForgeryToken]
     public ActionResult CreateEducation([Bind(Include = "DegreeEarned,Description,Institution,AcquiredDate,EducationStartDate,EducationEndDate,Department,InstitutionAddress,ReferenceName,ReferencePhoneNumber,ReferenceEmail")] Education ed, Command command)
     {
        if (ModelState.IsValid)
        {

            var user = userManager.FindById(User.Identity.GetUserId());
            user.ExpertInfo.Educations.Add(ed);
            db.SaveChanges();
            if (command == Command.SaveAndAddMore)
                return RedirectToAction("CreateEducation");
            else
                return RedirectToAction("CreateWorkExperience");
        }

        return View(ed);
    }

however / when I click on SaveAndAddMore button (the one with the default class), my controller method is invoked correctly with the correct command value and all is well. until I click on SaveAndContinue. On this instance, the controller is not passing any value for command.

The parameters dictionary contains a null entry for parameter 'command' of non-nullable type 'xxx.Models.Command' for method 'System.Web.Mvc.ActionResult CreateEducation(Experts.Models.Education, Experts.Models.Command)' in 'xxx.Areas.Expert.Controllers.ExpertInfoesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

The Command type is an enum

public enum Command { SaveAndAddMore, SaveAndNext, Save, Delete }

where have I gone wrong?

Community
  • 1
  • 1
Krishna
  • 2,451
  • 1
  • 26
  • 31
  • 2
    what is the Command class in the parameters? – Amr Elgarhy Jul 15 '15 at 21:47
  • 2
    have you tried with `string command` parameter in controller? – Amogh Jul 15 '15 at 21:48
  • @AmrElgarhy: please see my edit, just an enum – Krishna Jul 15 '15 at 21:49
  • @Amogh: no I havent, but would it make a difference, given that the default button (SaveAndAddMore) works. I am going to try anyway. – Krishna Jul 15 '15 at 21:50
  • 1
    @Krishna, As you said for SaveAndAddMore is working try changing value of second button to `SaveAndNext` as your Enum does not contain `SaveAndContinue` – Amogh Jul 15 '15 at 21:54
  • Hi Amogh - thanks for that my friend...it worked. I missed that detail. Please can you put this as an answer / I would like to accept this as an answer – Krishna Jul 15 '15 at 21:58

1 Answers1

3

As you said Command in Action parameter is an Enum:

public enum Command { SaveAndAddMore, SaveAndNext, Save, Delete }

So whatever values given to button is need to be same as Emum values, So your first button which is working having value same as Enum value i.e. SaveAndAddMore. Changing value of second button to SaveAndNext will work as you expected. So your div after change will be:

<div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="SaveAndAddMore" name="command" class="btn btn-default" />
            <input type="submit" value="SaveAndNext" name="command" class="btn" />

        </div>
    </div> 
Amogh
  • 4,453
  • 11
  • 45
  • 106