角模型MVC Html.TextBoxFOr结合模型、MVC、TextBoxFOr、Html

2023-09-14 23:08:30 作者:开学时,同桌是个什么货

我是模型依赖于ASP.net MVC5控制

  @ Html.TextBoxFor(型号=> model.OriginLocation.City,新{@class =表格控,data_ng_model =address1.City,test_change = }) 

所以,当页面加载文本框中输入的值约束,并应显示值与服务来与剃刀绑定控件,后者我可以操纵它改变角度模型此控件值。我有什么是文本框的负载空。我可以看到的价值,当我查看源代码,但它不会显示。

 <输入类=表格控NG-质朴的NG-有效的数据-NG-模式=address1.City数据-VAL =真正的数据val-长度=这个领域城市必须是50。最大长度的字符串数据-VAL长度最大=50ID =OriginLocation_CityNAME =OriginLocation.City测试变=类型=文本VALUE =瀚> 

JS片段

  app.controller('LocationCtrl',[$范围功能($范围){  $ scope.address1 = {标签:地址1'}; 
PHP MVC模型层的典型实现

解决方案

ngModel 已超过了最初设定的值precedence(它的值设置为 因为模型不存在)。看看这里...

http://jsfiddle.net/yApeP/

但你可以使用指定值 ngInit ...

http://jsfiddle.net/D7vh7/

这意味着你可以使用 ngInit 生成文本框​​时...

  @ Html.TextBoxFor(型号=> model.OriginLocation.City,    新{@class =表格控         data_ng_model =address1.City         test_change =,         data_ng_init =的String.Format(address1.City ='{0}',Model.OriginLocation.City.Replace(',@\\'))}) 

I have controls that are model tied to ASP.net MVC5

 @Html.TextBoxFor(model => model.OriginLocation.City, new { @class = "form-control", data_ng_model = "address1.City", test_change = "" })

So when the page loads the value of text box input is bound and should display value coming from service with Razor bound controls, latter i can manipulate that value which changes angular model for this control. What i have is textbox loads empty. I can see the value when I view source but its not displayed.

<input class="form-control ng-pristine ng-valid" data-ng-model="address1.City" data-val="true" data-val-length="The field City must be a string with a maximum length of 50." data-val-length-max="50" id="OriginLocation_City" name="OriginLocation.City" test-change="" type="text" value="Manheim">

js fragment

app.controller('LocationCtrl', ["$scope",
function ($scope) {
  $scope.address1 = { Label: 'address1' };

解决方案

ngModel has precedence over the value that is originally set (it's setting the value to "" because the model doesn't exist). Take a look here...

http://jsfiddle.net/yApeP/

But you can specify a value using ngInit...

http://jsfiddle.net/D7vh7/

Which means you can use ngInit when generating the textbox...

@Html.TextBoxFor(model => model.OriginLocation.City,
    new { @class = "form-control", 
         data_ng_model = "address1.City", 
         test_change = "",
         data_ng_init = string.Format("address1.City = '{0}'", Model.OriginLocation.City.Replace("'", @"\'"))  })