简体   繁体   中英

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?

I have an MVC app that I have made that uses angular only on certain pages. On those pages I need to pass the id of the part to angular so it knows where to start . I have played around with Viewbag and rendering child views but, I can't seem to get any of it to work.

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

namespace Nocore.Controllers

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


            return View();
        }
    }
}

Html view

          <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. Later I just feed the vars into my js/ts app.

@{
    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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM