【发布时间】:2015-07-15 21:43:08
【问题描述】:
我遵循SO 中建议的例程,即有两个同名按钮。
我的视图中有两个提交按钮。
<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>
在控制器中,我正在检查(或至少要检查)传递的命令的值
[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);
}
然而 / 当我点击 SaveAndAddMore 按钮(具有默认类的那个)时,我的控制器方法会使用正确的命令值正确调用,一切都很好。
直到我点击SaveAndContinue。在这种情况下,控制器没有为命令传递任何值。
参数字典包含参数的空条目 方法的不可为空类型“xxx.Models.Command”的“命令” 'System.Web.Mvc.ActionResult CreateEducation(Experts.Models.Education, Experts.Models.Command)' 在 'xxx.Areas.Expert.Controllers.ExpertInfoesController'。一个 可选参数必须是引用类型、可为空的类型,或者是 声明为可选参数。参数名称:参数
命令类型是枚举
public enum Command { SaveAndAddMore, SaveAndNext, Save, Delete }
我哪里出错了?
【问题讨论】:
-
参数中的Command类是什么?
-
你试过控制器中的
string command参数吗? -
@AmrElgarhy:请看我的编辑,只是一个枚举
-
@Amogh:不,我没有,但考虑到默认按钮(SaveAndAddMore)有效,它会有所不同。反正我要试试。
-
@Krishna,正如您所说的 SaveAndAddMore 正在尝试将第二个按钮的值更改为
SaveAndNext,因为您的枚举不包含SaveAndContinue
标签: c# asp.net-mvc asp.net-mvc-5