MVC3剃刀的ViewData [英] MVC3 Razor ViewData
问题描述
我有一个在线的形式。例如,它要求名字和姓氏。当他们提交我把它们发送到其他视图。第二个观点有几个文本框(密码,电子邮件地址,用户名),它也有名字和姓氏虽然。如果他们填写的第一个表单,并在名字/姓氏我想第二个形式,因为它们已经在一个充满显示这些填充值。
I have an online form. for example it asks for first name and last name. When they submit I send them to another view. this second view has a few more textboxes (password, email address, username) it also has first name and last name though. If they fill out the first form and fill in firstname/lastname i want the second form to display these values since they have already been filled in.
在第一种形式,我把所有的填写信息到TempData的[项]
in the first form I am putting all the filled out information into TempData["entry"]
在第二种形式我这样做检查。
in the second form i am doing this check.
if (TempData["entry"] != null)
{
var _model = (AccountInformationModel)TempData["entry"];
ViewData["_firstName"] = _model.NameFirst;
ViewData["_lastName"] = _model.NameLast;
}
return View("Register");
我想在我看来,我是一个有点困惑如何在一个文本框显示此数据上。我有这个在我看来,但它似乎没有奏效。
i guess in my view im a bit confused on how to display this data in a textbox. I have this in my view but it doesnt seem to be working.
<div class="editor-label">
@Html.LabelFor(m => m.FirstName)
</div>
<div class="editor-field">
@Html.TextBox("FirstName", ViewData["FirstName"])
@Html.ValidationMessageFor(m => m.FirstName)
</div>
显然这行...
clearly the line that says...
@Html.TextBox("FirstName", ViewData["FirstName"])
不工作..
推荐答案
您正在将数值为_FirstName
You're assigning the value to "_firstName"
ViewData["_firstName"] = _model.NameFirst;
,然后试图以显示名字
And then trying to display "FirstName"
@Html.TextBox("FirstName", ViewData["FirstName"])
试着将其更改为:
Try changing it to:
@Html.TextBox("FirstName", ViewData["_firstName"])
其他的方式做到这一点,以避免类似的失误是定义一个新的类,它包含所有视图数据键。这种方式的字符串被包含在一个位置,使得维护和变化更容易。
The other way to do this to avoid mistakes like this is to define a new class that will contain all your view data keys. This way your strings are contained in one location, making maintenance and changes easier.
public static class ViewDataKeys
{
public const string FirstName = "_firstName";
}
然后,当你要访问它,你可以使用
Then when you want to access it, you can use
ViewData[ViewDataKeys.FirstName] = _model.NameFirst;
这篇关于MVC3剃刀的ViewData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!