0

Is it not possible to use strongly typed objects in asp.net mvc to initialize ng-model properties as currently the view value is getting cleared as soon as ng-model property is getting bind to view.I know it will work if I initialize that value form model but that would mean I will have to make a get request just to pull the model value ,but what if I want to leverage existing strongly typed model binding feature of asp.net mvc ??

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73

2 Answers2

1

You can make you use of ng-init. For example you have:

 <input type="text" ng-model="name">

You can use it as:

<input type="text" ng-model="name" ng-init="name='@Model.Name'">

Which on the client side will get converted to:

<input type="text" ng-model="name" ng-init="name='Abdel'">

And you will be able to use it... Hope this helps!

Abdel Raoof Olakara
  • 19,223
  • 11
  • 88
  • 133
  • yes **ng-init** can be one of the option but I want a generic and more cleaner way of doing this , so that view takes the owners ship of initializing model's property , basically I don't want to repeat this for each model property. - @Abdel Raoof – Mayank Singh Apr 24 '15 at 11:28
0

We can do this by using a value provider, which can be injected in controller or service:

  var app = angular.module('app', []);

  app.value('mystuff', @Html.Raw(JsonConvert.SerializeObject(Model)));

  app.controller('HomeController', ['mystuff', function(mystuff) {

  }]);
Sergiu Luca
  • 1
  • 1
  • 1