Angular ASP.NET MVC Binding

前端 未结 1 1539
暗喜
暗喜 2020-12-31 11:33

In our MVC 5 project we use Angular. The following Razor works nicely:

 @Html.EditorFor(x => x.FirstName,
          new { required = \"required\", ng_mode         


        
1条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-31 12:10

    There is no need to separate the model into individual fields when you bind to scope. Instead you should bind the entire model:

     $scope.model = @Html.Raw(Json.Encode(Model));
    

    This would render to the client as:

     $scope.model = { FirstName: 'John', LastName:'Doe', etc };
    

    Then you can bind your input fields as:

    @Html.EditorFor(x => x.FirstName,
          new { required = "required", ng_model = "model.FirstName" })
    

    Personally, I think its cleaner not to use @Html, in favor of simple HTML:

    
    

    In Angular, you don't really need an id anymore.

    0 讨论(0)
提交回复
热议问题