簡體   English   中英

Asp.net MVC ActionResult如何檢測可選參數?

[英]Asp.net MVC ActionResult how to detect optional parameters?

我想檢測是否從URL將可選參數傳遞給我的案例字符串Professional的ActionResult

site.com/Account/Register/Professional應該設置模型IsProfessional = true

site.com/Account/Register/應設置模型IsProfessional = false

使用以下代碼字符串professional始終為null。

知道如何解決嗎?

   public ActionResult Register(string professional)
    {
        // DETECT HERE
        RegisterViewModel model = new RegisterViewModel();
        if (!string.IsNullOrWhiteSpace(professional) && professional.ToLower() == "professional")
        {
            model.IsProfessional = true;
        }
        return View();
    }



  public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

與其使用專業人士,不如使用與您的MapRoute相匹配的ID。 試試下面的代碼:

    public ActionResult Register(string id)
    {
        var model = new RegisterViewModel();
        if (!string.IsNullOrWhiteSpace(id) && id.ToLower() == "professional")
        {
            model.IsProfessional = true;
        }
        else
        {
            model.IsProfessional = false;
        }
        return View();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM