Using AngularJS with ng-model all my fields are cleared after postback from mvc controllers. Can prevent this somehow and put the values from the postback into the model? Every time the user make a postback with an validation error all fields get cleared because of the ng-model. Any ideas?
@using (Html.BeginForm("Register", "Account", FormMethod.Post))
{
@Html.TextBoxFor(x => x.Register.Username, new { @ng_model = "username" })
@Html.TextBoxFor(x => x.Register.Password, new { @ng_model = "password" })
}
[AllowAnonymous]
public ActionResult Register(RegisterModel viewModel)
{
return View(viewModel);
}
答案 0 :(得分:5)
这是因为当Angular启动时, $ scope 上的用户名和密码值为空,它们将覆盖您的视图所设置的内容。
ng-init可以解决您的问题
考虑类似
的内容@Html.TextBoxFor(x => x.Register.Username, new { @ng_model = "username", @ng_init = "username ='" + Model.Register.Username + "'" })
@Html.TextBoxFor(x => x.Register.Password, new { @ng_model = "password", @ng_init = "password ='" + Model.Register.Password + "'" })
干杯