首页 > 解决方案 > 如何在 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();
    }

标签: asp.net-mvcmodel-view-controllerasp.net-mvc-5actionresult

解决方案


If you are using c# 4.0 or higher, you can use this:

public ActionResult Details(string name = "xxx")
  {
    // some code ...
    return View();
  }

推荐阅读