簡體   English   中英

重載ActionResult

[英]overload ActionResult

這是我的動作“索引”
當我第一次進入頁面時,我沒有“ pageParent”
所以我沒有頁面。
就像我輸入“ http:// localhost:50918 / Page?pageParent = 1”一樣,它就進入了。
如何使“ http:// localhost:50918 / Page”正常工作?

public ActionResult Index(int pageParent)
    {
        var id = pageParent;

        var pages = (from p in db.pages
                     where p.pageParent == id 
                         select p);

        PageParentModel model = new PageParentModel();
        model.page = pages;
        model.pageParent = id;

        return View(model);
    }

像這樣修改您的動作

public ActionResult Index(int? pageParent) {
    // this way your pageParent parameter is marked to be nullable
    // dont forget to check for the null value in code
}

您也可以設置默認值以在查詢字符串中未提供參數的情況下使用-例如:

public ActionResult Index([DefaultValue(1)] int pageParent) {
}

暫無
暫無

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

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