C# 将参数从url传递到视图
我注意到mvc默认项目中有一些东西让我想知道它是如何工作的。当我创建一个带有单个用户帐户身份验证的ddefault MVC项目时,VisualStudio用两个“ResetPassword”操作构建了一个AccountController。通过GET请求接受字符串参数的函数。操作如下所示:C# 将参数从url传递到视图,c#,asp.net,asp.net-mvc,razor,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Razor,Asp.net Mvc 5,我注意到mvc默认项目中有一些东西让我想知道它是如何工作的。当我创建一个带有单个用户帐户身份验证的ddefault MVC项目时,VisualStudio用两个“ResetPassword”操作构建了一个AccountController。通过GET请求接受字符串参数的函数。操作如下所示: // GET: /Account/ResetPassword [AllowAnonymous] public ActionResult ResetPassword(string
// GET: /Account/ResetPassword
[AllowAnonymous]
public ActionResult ResetPassword(string code)
{
return code == null ? View("Error") : View();
}
视图如下所示:
@model SISGRAD_MVC.Models.ResetPasswordViewModel
@{
ViewBag.Title = "Reset password";
}
<h2>@ViewBag.Title.</h2>
@using (Html.BeginForm("ResetPassword", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Reset your password.</h4>
<hr />
@Html.ValidationSummary("", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Code)
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Reset" />
</div>
</div>
@model SISGRAD\u MVC.Models.ResetPasswordViewModel
@{
ViewBag.Title=“重置密码”;
}
@ViewBag.Title。
@使用(Html.BeginForm(“ResetPassword”,“Account”,FormMethod.Post,new{@class=“form horizontal”,role=“form”}))
{
@Html.AntiForgeryToken()
重置密码。
@Html.ValidationSummary(“,new{@class=“text danger”})
@Html.HiddenFor(model=>model.Code)
@LabelFor(m=>m.Email,新的{@class=“col-md-2控制标签”})
@TextBoxFor(m=>m.Email,新的{@class=“form control”})
@LabelFor(m=>m.Password,新的{@class=“col-md-2控制标签”})
@Html.PasswordFor(m=>m.Password,新的{@class=“form control”})
@LabelFor(m=>m.ConfirmPassword,新的{@class=“col-md-2控制标签”})
@Html.PasswordFor(m=>m.ConfirmPassword,new{@class=“form control”})
我使用URL中的代码访问操作,获取样式,视图知道从URL初始化模型属性。有趣的一点是,只有使用@Html.HiddenFor()
,这才有效。这是如何工作的,以及视图如何知道何时从URL提取数据,何时不从URL提取数据?,因为您的方法是
public ActionResult ResetPassword(string code)
DefaultModelBinder
将code
的值添加到ModelState
HiddenFor(m=>m.code)
方法使用来自ModelState
的值,而不是来自模型的值(如果它们存在的话),因此它将进行渲染
<input type="hidden" name="Code" id="Code" value="###" />
<form action="Account/ResetPassword/###" ... >
它将抛出“未设置为对象实例的对象引用”。异常
<div>@ViewData.ModelState["Code"].Value.AttemptedValue</div>
与使用以下代码的HiddenFor(m=>m.code)
相反(请参阅
另外请注意,如果使用url:“Account/ResetPassword/{code}”
定义路由,则不需要在视图中添加隐藏的输入。默认情况下,它将作为路由值添加-将呈现BeginForm()
方法
<input type="hidden" name="Code" id="Code" value="###" />
<form action="Account/ResetPassword/###" ... >
如果您使用Account/ResetPassword/{code}
定义了路由,那么您甚至不需要隐藏的输入(默认情况下,它将添加到rote参数中)路由值、查询字符串值和模型中的所有值,并添加到ViewDataDictionary
,html帮助程序使用该字典设置表单的关联值controls@StephenMuecke关于第二篇评论,我也怀疑这一点,但调试器中的一步一步显示了空的ViewDataDictionaryugh视图渲染。此外,为什么它只适用于Html.HiddenFor()?我尝试了DisplayFor,但它不起作用。不久前,我在一个不错的博客上找到了一个更好的解释,让我看看是否可以找到它:)@StephenMuecke关于第一条评论,谢谢你的提示,但我的问题更多的是对框架内部工作原理的好奇,以提高我的理解。
<form action="Account/ResetPassword/###" ... >