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?

6
  • 2
    what is the Command class in the parameters? Commented Jul 15, 2015 at 21:47
  • 2
    have you tried with string command parameter in controller? Commented Jul 15, 2015 at 21:48
  • @AmrElgarhy: please see my edit, just an enum Commented Jul 15, 2015 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. Commented Jul 15, 2015 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 Commented Jul 15, 2015 at 21:54

1 Answer 1

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> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.