Asp.net mvc 如何使用ViewBag值设置模型属性?
我想在ViewBag中设置值,如下面所示Asp.net mvc 如何使用ViewBag值设置模型属性?,asp.net-mvc,entity-framework-4,asp.net-mvc-4,Asp.net Mvc,Entity Framework 4,Asp.net Mvc 4,我想在ViewBag中设置值,如下面所示 public ActionResult Register(Guid accountId) { ViewBag.AccountId = accountId; return View("Register", "Account"); } @Html.HiddenFor(m => m.AccountId, new { id = "hdaccountid", value = ViewBag.Acc
public ActionResult Register(Guid accountId)
{
ViewBag.AccountId = accountId;
return View("Register", "Account");
}
@Html.HiddenFor(m => m.AccountId, new { id = "hdaccountid", value = ViewBag.AccountId })
现在在注册视图中,我可以像下面那样设置吗
public ActionResult Register(Guid accountId)
{
ViewBag.AccountId = accountId;
return View("Register", "Account");
}
@Html.HiddenFor(m => m.AccountId, new { id = "hdaccountid", value = ViewBag.AccountId })
我想用ViewBag值更新模型属性。所以当我想在控制器内更新模型时,模型属性包含正确的值
我不想使用ModelView。请建议 如果我能理解
如果未将模型传递给视图,则不能使用HiddenFor
看法
控制器
// pass parameter to view
[HttpGet]
public ActionResult Register(Guid accountId)
{
ViewBag.AccountId = accountId;
return View();
//return View("Register", "Account");
}
// post parameter from view
[HttpPost]
public ActionResult Register(Guid accountId, string id)
{
// Get model from DB and change it
// model.accountId = accountId;
...
}
如果我有
MoveToRegister(Guid accountId)
将视图中的accountId设置为u所述的隐藏状态,那么另一个Register(UserModel Model)
从这个更新数据库中?如果你想从视图中获取模型,你应该将模型传递给视图。@AliRıza AdıyahşI:如果我想,将隐藏的值放入控制器中,那么我如何才能获取该值。实际上,我有两个ActionResult-->从一个it传递accountid到一个视图。然后输入一些值-->提交表单-->调用另一个操作结果并将数据保存到数据库中,包括隐藏字段值。