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

2 Answers 2

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!

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

1 Comment

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

  }]);

Comments

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.