这是我的Change Pass ViewModel。
public class ChangePasswordViewModel
{
[Required]
[DataType(DataType.Password)]
[Display(Name = "Current password")]
public string OldPassword { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
我将redirectAction重定向到具有登录ID的ChangePass控制器,如>>
return RedirectToAction("ChangePassword", new { id = loginuser[0].PkUserAcc });
在我的Change Pass获取方法>>
中public ActionResult ChangePassword(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
tblUserAcc tbluseracc = db.tblUserAccs.Find(id);
if (tbluseracc == null)
{
return HttpNotFound();
}
else
{
return View();
}
}
我在ChangePassword视图>>
中使用了ChangePasswordViewModel@model IBS.Models.ChangePasswordViewModel
这样的网址>>
http://localhost:63855/User/ChangePassword/2
我的问题是>>
我可以从Post方法获得LoginID(2)吗?
需要传递LoginID才能查看表单ChangePass(获取方法)以及如何传递?
答案 0 :(得分:1)
为模型添加属性
public class ChangePasswordViewModel
{
public int ID { get; set; } // add this
[Required]
[DataType(DataType.Password)]
[Display(Name = "Current password")]
public string OldPassword { get; set; }
然后在你的GET方法中
public ActionResult ChangePassword(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
tblUserAcc tbluseracc = db.tblUserAccs.Find(id);
if (tbluseracc == null)
{
return HttpNotFound();
}
ChangePasswordViewModel model = new ChangePasswordViewModel();
model.ID = id;
return View(model);
}
如果您使用defaults: new { controller = "..", action = "..", id = UrlParameter.Optional }
的默认路由,则ID
将添加到路由值中,并且在您回发时模型将与ID绑定。如果没有,那么您需要为该值添加隐藏输入(或将其添加到路线值)
答案 1 :(得分:0)
您可以使用Viewbag
首先在控制器中设置。
..
ViewBag.LoginID = tbluseracc.Id;
return View();
并在您的视图中
...
<input id="loginId" type="hidden" value="@ViewBag.LoginID"/>
从那时起你可以在post方法中再次传递它。