简体   繁体   English

ASP.net MVC 路由参数到 Angular

[英]ASP.net MVC Route Parameters to Angular

Updated I have been trying to figure this out for a couple of days now.更新了我几天来一直试图解决这个问题。 I have found plenty of examples of how to pass angular route parameters to MVC but, how do I go about doing it the other way?我已经找到了很多关于如何将角度路由参数传递给 MVC 的示例,但是,我如何以另一种方式进行呢?

I have an MVC app that I have made that uses angular only on certain pages.我有一个我制作的 MVC 应用程序,它仅在某些页面上使用 angular。 On those pages I need to pass the id of the part to angular so it knows where to start .在这些页面上,我需要将部件的 id 传递给 angular,以便它知道从哪里开始。 I have played around with Viewbag and rendering child views but, I can't seem to get any of it to work.我玩过 Viewbag 并渲染子视图,但是,我似乎无法让它工作。

It seems like it should be so simple.好像应该这么简单。 I am really overthinking it or completely missing the obvious.我真的想太多了,或者完全错过了显而易见的事情。 I tend to do that sometimes.有时我倾向于这样做。

Here is what I am currently trying.这是我目前正在尝试的。

ASP.net Controller ASP.net 控制器

namespace Nocore.Controllers

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


            return View();
        }
    }
}

Html 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>

I guess this was down voted because I didn't include code.我想这被否决了,因为我没有包含代码。 Above is the code I have been trying.以上是我一直在尝试的代码。

Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks.谢谢。

Here's an example code placed in the Razor view cshtml that I use to capture the MVC route values.这是放置在 Razor 视图 cshtml 中的示例代码,我用它来捕获 MVC 路由值。 Later I just feed the vars into my js/ts app.后来我只是将 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