asp.net-mvc - 如何在 Asp.Net MVC 的 ActionResult 方法中设置可选参数值?
问题描述
当用户点击按钮时,它会重定向到另一个控制器动作方法,有或没有参数值
<button type="button" onclick="location.href='http://localhost/ProjectName/Customer/Details'" >Go to Details</button>
<button type="button" onclick="location.href='http://localhost/ProjectName/Customer/Details?name=xxx'" >View Details</button>
客户控制器
public ActionResult Details() // i don't know how to pass optional parameter value
{
// some code ...
return View();
}
解决方案
If you are using c# 4.0 or higher, you can use this:
public ActionResult Details(string name = "xxx")
{
// some code ...
return View();
}
推荐阅读
- php - 如何从mysql数据库中提取while循环中的数组值
- c# - 在 IIS Express,VS 2017 中运行网站时出现未处理的访问异常
- php - 登录时不断收到“密码错误”
- algorithm - 如何解决 K8s pod 之间的竞争条件
- python - 如何为 Char 字段传递值 Many2one ?(奥多 11)
- javascript - 第一次单击不会记录所有信息,但第二次单击会记录
- firebase - 自定义域的 Firebase HTTP 到 HTTPS NGINX 重定向丢弃 GET 的 POST 方法
- android - 将应用程序包上传到 Play 管理中心时出现导入错误
- javascript - 在 UI 中重复 React 组件
- c++ - 顶点着色器错误 C5145:必须使用 QShaderProgram 写入 gl_Position