簡體   English   中英

ASP.net MVC 路由參數到 Angular

[英]ASP.net MVC Route Parameters to Angular

更新了我幾天來一直試圖解決這個問題。 我已經找到了很多關於如何將角度路由參數傳遞給 MVC 的示例,但是,我如何以另一種方式進行呢?

我有一個我制作的 MVC 應用程序,它僅在某些頁面上使用 angular。 在這些頁面上,我需要將部件的 id 傳遞給 angular,以便它知道從哪里開始。 我玩過 Viewbag 並渲染子視圖,但是,我似乎無法讓它工作。

好像應該這么簡單。 我真的想太多了,或者完全錯過了顯而易見的事情。 有時我傾向於這樣做。

這是我目前正在嘗試的。

ASP.net 控制器

namespace Nocore.Controllers

    public class ThreadedStyliController : Controller
    {
        // GET: Threaded
        public ActionResult Threaded(string specThread)
        {
            ViewBag.Thread=specThread;


            return View();
        }
    }
}

html視圖

          <div class="col-3 ">
                <label class="sr-only " for="Thread">Thread</label>
                <div class="input-group mb-2 mr-sm-2 mb-sm-0 ">
                    <div class="input-group-addon ">
                        <span class="input-group-text ">Thread</span>
                    </div>
                    <Select class="form-control selectpicker" style="width:100px; display: inline-block " id="currentThread
                    " name="currentThread " size="1 " ng-model="currentThreadId " ng-change="setCurrentthread()">
                        <option ng-repeat="t in threads | orderBy: 'id' " ng-selected="@ViewBag.Thread">{{t.id}}</option>
                    </Select>
                </div>
            </div>

我想這被否決了,因為我沒有包含代碼。 以上是我一直在嘗試的代碼。

任何幫助將不勝感激。 謝謝。

這是放置在 Razor 視圖 cshtml 中的示例代碼,我用它來捕獲 MVC 路由值。 后來我只是將 vars 輸入到我的 js/ts 應用程序中。

@{
    var StartDate = ViewContext.RouteData.Values["StartDate"] != null ?
        ViewContext.RouteData.Values["StartDate"].ToString().Replace('-', '/') : DateTime.Now.AddMonths(-3).ToShortDateString();
    var EndDate = ViewContext.RouteData.Values["StartDate"] != null ?
        ViewContext.RouteData.Values["EndDate"].ToString().Replace('-', '/') : DateTime.Now.ToShortDateString();

}

<script>

    let startDate = "@StartDate",
        endDate = "@EndDate";

    function stuff(var1, var2){
    //do stuff
    };

    stuff(startDate, endDate);

</script>

暫無
暫無

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

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