我正在尝试将视图中的操作链接中的参数传递给控制器。行动链接如下:
<div>
@Html.ActionLink("Email Sales", "ContactForm", "ContactForm",null, new {reportID = 1});
@Html.ActionLink("Email Tech Support", "ContactForm", "ContactForm", null, new { reportID = 2 });
@Html.ActionLink("Email Accounting", "ContactForm", "ContactForm", null, new { reportID = 3 });
@Html.ActionLink("Email Marketing", "ContactForm", "ContactForm", null,new { reportID = 4 });
</div>
在我的控制器中,代码如下所示:
public ActionResult ContactForm(int reportid)
{
ContactFormVM form = new ContactFormVM();
switch (reportid)
{
case 1:
form.SendTo = ConfigurationManager.AppSettings["SalesEmail"];
break;
case 2:
form.SendTo = ConfigurationManager.AppSettings["TechSupportEmail"];
break;
case 3:
form.SendTo = ConfigurationManager.AppSettings["MarketingEmail"];
break;
case 4:
form.SendTo = ConfigurationManager.AppSettings["AccountingEmail"];
break;
default:
break;
}
return View(form);
}
还有很多其他问题,我已经全部阅读过了。
我使用谷歌浏览器检查代码生成的元素链接,看起来不错。见下文:
<a href="/SoundHelpPortal/ContactForm/ContactForm" reportid="1">Email Sales</a>
如果我改变我的函数以获取一个字符串,我得到一个空值,如果它是整数,我得到关于null参数的错误。