MR.*_*ABC 0 javascript asp.net-mvc asp.net-mvc-4 angularjs
将AngularJS与ng-model一起使用后,我的所有字段都会在从mvc控制器回发后清除.可以以某种方式阻止这种情况并将回发值放入模型中吗?每次用户使用验证错误进行回发时,由于ng-model,所有字段都会被清除.有任何想法吗?
@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);
}
Run Code Online (Sandbox Code Playgroud)
这是因为当Angular启动时,$ scope上的用户名和密码值为null,它们将覆盖视图设置的值.
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 + "'" })
Run Code Online (Sandbox Code Playgroud)
干杯
| 归档时间: |
|
| 查看次数: |
584 次 |
| 最近记录: |