3

Is it possible to use AngularJS variable as Route Parameter in Razor Syntax like this?

<tr data-ng-repeat="job in jobs">
    <td>
        <a href="@Url.RouteUrl("editJob", new { id =  job.JobID })">
            <i class="glyphicon glyphicon-edit" style="color:#808080"></i>
        </a>
    </td>
</tr>

Right now I am using this.

<a href='@Url.Content("~/job/edit/"){{job.JobId}}'>
4
  • is it working for you? Commented Feb 9, 2016 at 9:07
  • first one is not working and the second one is working. The first one is much cleaner Commented Feb 9, 2016 at 9:09
  • then use that. If works fine. As it understood string find replace in backend works for second condition. Commented Feb 9, 2016 at 9:12
  • I was asking a question on whether its possible. Ok? Commented Feb 9, 2016 at 9:13

2 Answers 2

5

I'd say no, since Razor is rendered server side and at that time Angular variables don't exist. Meaning new { id = job.jobID } will result in { id : null }.

Your second solution is working because the angular string is concatenated right after the Razor part ends.

Sign up to request clarification or add additional context in comments.

Comments

1

I recommend neither solutions because mixing multiple templates is always a bit dangerous. The way our team solves this is

  1. pass in all razor dependencies on in a separate script block with a dynamically created constant on in the ng-init (if only used in simple cases)
  2. only use these in your angular template

A bit like this (pseudo-code, example using ng-init)

<div ng-app="..." ng-init="config = { rootUrl: '@Url.Content("~/job/edit/")', otherValue: ''}">

  <a href="{{config.rootUrl + job.JobId}}">...</a>
</div>

2 Comments

yes. the thing is that I want to use angular and the asp.net routing together.
@naveen this can be done fairly well like this. I'm assuming you want to use html5 pushstate for angular but map the same urls to the server with asp.net routing, correct? So I'm understanding your core question to be "can you use the same url-config to assemble the link url in razor/angular" - would that hit it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.