[英]MVC Value not posting back to controller
This is weird, or at least i think.这很奇怪,或者至少我认为。 I have a simple password change form, which has a control for taking the user input for the password.我有一个简单的密码更改表单,它有一个控制用户输入密码的控件。 Now, the problem is whatever the user enters, the view only posts back an empty string.现在,问题是无论用户输入什么,视图都只回发一个空字符串。
This is the control on my view这是对我的看法的控制
@Html.PasswordFor(model => model.Password)
Model attribute:模型属性:
[Required]
[DataType(DataType.Password)]
[StringLength(20, MinimumLength = 10)]
[Display(Name = "Password")]
public string Password { get; set; }
[HttpGet]
public ActionResult Change(int userid)
{
try
{
var model = GetUser(userid);
return View(model);
}
catch (Exception e)
{
//log error
throw;
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Change(User model)
{
try
{
model = ChangeUserCredentials(model); //break point here
return View(model);
}
catch (Exception e)
{
//log error
throw;
}
}
What I have tried:我尝试过的:
fwiw, it was working fine until a few days back, nothing changed in the view or the code in the controller. fwiw,直到几天前它都运行良好,视图或控制器中的代码没有任何变化。
add a parameter to your password helper like this:向您的密码助手添加一个参数,如下所示:
@Html.PasswordFor(model => model.Password,new{@name="mypassword"})
then add this parameter to your action method然后将此参数添加到您的操作方法中
public ActionResult Change(User model,string mypassword)
this will work!这会奏效! but it's not a really good way.但这不是一个很好的方法。 if you want to do it right you must be sure you did Model Binding right.如果你想把它做对,你必须确保你做了正确的模型绑定。
I know this sounded stupid.我知道这听起来很愚蠢。 But this is what fixed the issue, just in case anyone else faces the same:但这就是解决问题的方法,以防万一其他人面临同样的问题:
1) Per this post , I added new { value = Model.CurrentPassword }) to the password attribute in the view 2) I am deliberately setting the password to 'null' in the GET method 1)根据这篇文章,我在视图中的密码属性中添加了新的 { value = Model.CurrentPassword }) 2)我故意在 GET 方法中将密码设置为“null”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.