T4MVC in javascript jquery url property

Oct 24, 2012 at 8:34 PM

Hi,

is it possible to build url to controller/actions inside js using T4MVC?

<script type="text/javascript">   
         $(function () {       
                   $('#Make_Id').cascade({           
                           url: '/Model/ListFromMake',
                        //T4MVC HERE=> url: '@Html.Action(MVC.Model.ListFromMake(Model.Make_Id))',
                           paramName: 'makeId',           
                           childSelect: $('#Model_Id')        });    });
</script>

 

Also, what do you recomend for Visual Studio 2012 to auto run the T4MVC tool on build?

Thanks.

Coordinator
Oct 24, 2012 at 10:35 PM

Maybe the same as this one?

You can use AutoT4MVC on VS2012.

Oct 25, 2012 at 11:18 AM

Don't you still have "Magic Strings" from this aproach?

Regards.

Coordinator
Oct 25, 2012 at 3:05 PM

You mean "id" in the example? That could be replaced by a T4MVC thing, e.g. MVC.Thing.DeleteActionParams.id.

Oct 29, 2012 at 10:19 AM

I mean replace the url '/Model/ListFromMake' by an T4MVC thing with the param inside.

Coordinator
Oct 29, 2012 at 4:23 PM

Did you try what I had suggested in that SO question? That does get rid of the hard coded path.

Oct 30, 2012 at 10:15 AM

Hi, Sorry I did try your suggestion but regarding the url, I didn't found a way to generated it using T4MVC:

For the paramName: I found it: '@MVC.Model.Actions.ListFromMakeParams.makeId'

But for the url (to generate '/Model/ListFromMake') I did not found any way to get there;  '@MVC.Model.????'

Am I missing something here?

Thanks

Regards.

Coordinator
Oct 30, 2012 at 9:19 PM

What exactly did you try and what did it do? That thread suggests using @Url.Action(MVC.YourController.YourAction().AddRouteValue(etc...)).

Oct 31, 2012 at 10:12 AM

Hi, I'm trying to generate the Controller/Action url for the cascade javascript function.

This is the method I'm trying to call with the cascade function:

        [HttpPost]       
public virtual ActionResult ListFromMake(int makeId)       
{           
var modelsDto = _modelService.ListModelByMake(makeId);
var viewModel = Mapper.Map<IList<ModelDto>, IList<ModelViewModel>>(
return Json(viewModel);       
}

I was able to build using T4MVC the paramName of the cascade function using '@MVC.Model.ListFromMakeParams.makeId'

Now I want to generate the url parameter for the cascade function using the T4MVC but I'm not getting there.

I tried url: '@Html.Action(MVC.Model.ListFromMake().AddRouteValue("makeId","a"))', but I'm getting the error: "A public action method 'ListFromMake' was not found on controller 'Heelp.Controllers.ModelController'." and I don't understand why I need parameters in the AddRouteValue because I just want to generate the Controller/Action string, "/Model/ListByMake".

Regards.

Coordinator
Oct 31, 2012 at 11:45 PM

That's strange. Why is it not finding that ListFromMake public method? Or are you able to use it outside of this script scenario?

I would really need to look at the complete app to see what you're doing. e.g. If you share a minimal repro on github, I can take a look.

Nov 2, 2012 at 10:51 AM

Hi, maybe I'm not using T4MVC properly.

How do you generate the string Controller/Action -> "/Model/ListByMake" using T4MVC?

Regards.

Nov 2, 2012 at 3:46 PM
Edited Nov 2, 2012 at 4:12 PM

Hi,

I finally found the answer: 

url: '@Url.Action(MVC.Model.ListFromMake())',

With this line I'm able to generate the code:

url: '/Model/ListFromMake'

By the way, I would like to ask a question, from the test I made, when I rename the Action in the Model Controller, the code gets underlined in the Razor Page, but when I compile, I don't get any error.

Is there a way to rename also the action name in the T4MVC code to avoid errors when the actions gets renamed in the controller?


Thanks for your help.

Regards.

Coordinator
Nov 2, 2012 at 6:06 PM

See the doc page for various useful info on T4MVC.

If you make a change to your action, you need to rerun T4MVC (right click .tt file, run custom tool). Or use a tool like this one.

Nov 2, 2012 at 6:08 PM

I'm using it but it does not detect the change in compilation

Coordinator
Nov 2, 2012 at 6:10 PM

You mean rerunning the custom tool has no effect? I have not seen that and would need to see a repro app.

Nov 2, 2012 at 6:11 PM

I have installed the tool on the extension manager of the VS2012.

Do I need to do anything else?

Coordinator
Nov 2, 2012 at 6:22 PM

Sorry, we're talking about different things here. If you right click .tt file, and choose run custom tool, does it properly re-generate the code?

If that works but it's just AutoT4MVC that doesn't, then it may be worth asking that question on that project's page (https://github.com/bennor/AutoT4MVC). It's a separate project that I don't own. The tool did work for me when I tried it a while back.

Nov 2, 2012 at 6:28 PM

Ok, let me detail the steps I made after create the code @Url.Action(MVC.Model.ListFromMake()) 

1º I went to the Model Controller

2º Rename the Action ListFromMake to ListFromMake2

3º Compile -> No error detected

4º I went to the View and the code @Url.Action(MVC.Model.ListFromMake()) was underlined

5º I tried your sugestion and run the custom tool but the line @Url.Action(MVC.Model.ListFromMake()) remain the same

 

I was expecting that the compile process would generate the code @Url.Action(MVC.Model.ListFromMake2()) 

No?

Coordinator
Nov 2, 2012 at 6:30 PM

No, it never changes your code. It just changes the generated files, which then allow you to write the correct thing. Look at the generated files under t4mvc.tt and see the diff after the change.

Nov 2, 2012 at 6:33 PM

Yes I saw that, but I was expecting that having T4MVC would help me detecting changes in the code to avoid errors like the example above.

So, I have no way to prevent errors in the views if I change anything in the controllers? Like if I had Magic Strings.

What should be the procedure to avoid errors in the views after any change using T4MVC?

Coordinator
Nov 2, 2012 at 6:37 PM

You can use MvcBuildViews if you want to compile all your views when you build. That'll catch those errors, but does slow down the build a bit.

Nov 2, 2012 at 6:42 PM

Ok thanks.

When you say "It helps make your MVC code much more maintainable", in what sice do you mean?