1

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.

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"])

doesnt work..

gevjen
  • 675
  • 4
  • 14
  • 30
  • Just curious, why not pass an instance of the model containing the first and last name to your second view and then use `@Html.TextBoxFor(m => m.FirstName)`? – Jeff Ogata Mar 23 '11 at 19:37
  • @adrift...just tried out your suggestion and it works perfectly..i like both ideas here...but yours seems to take advantage of the idea of mvc pretty good...thanks for the suggestion! – gevjen Mar 23 '11 at 20:16

1 Answers1

9

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;
Brandon
  • 68,708
  • 30
  • 194
  • 223
  • brandon...thanks..stupid mistake..always good to have another set of eyes i guess, i was on the right track though. – gevjen Mar 23 '11 at 19:26
  • @gevjen, no problem. As a side note, you may want to consider using constants rather than strings to avoid errors like this. – Brandon Mar 23 '11 at 19:28
  • @brandon...never done that before, nor have i heard it before..ill look it up, but if you have any link literature pass it along please...thanks! – gevjen Mar 23 '11 at 19:30
  • @gevjen, see answer for alternate solution. – Brandon Mar 23 '11 at 19:32
  • ah..follow you..much better way to do that..Ill make that change..thanks for teaching me a better way to do this..! – gevjen Mar 23 '11 at 19:34
  • @gevjen, no problem. Formatting in comments is a little hard to read, so I will just add it to my answer instead. – Brandon Mar 23 '11 at 19:35
  • By the way, ViewData["_firstName"] throws an error in MVC 4, so instead you can do: ViewData["_firstName"].ToString() – IIS7 Rewrite May 18 '13 at 01:15