7

I'n new at this, so apologies if this isn't explanatory enough. I want to prepopulate a field in a form in asp.net mvc 3. This works;

@Html.TextBox("CompName", null, new { @value = ViewBag.CompName })

But when I want to prepopulate it with a value and send that value to my model, like this;

 @Html.TextBoxFor(model => model.Comps.CompName, null, new { @value = ViewBag.CompName })

It won't work. Any ideas?

Thanks in advance!

2 Answers 2

16

So, I would suggest is to move to using viewmodels rather than the ViewBag. I made a folder in my project called ViewModels and then under there make some subfolders as appropriate where I put my various viewmodels.

If you create a viewmodel class like so:

public class MyViewModel
{
   public string CompName { get; set; }
}

then in your controller action you can create one of those and populate it, maybe from some existing model pulled from a database. By setting that CompName property in the viewmodel, it'll have that value in your view. And then your view can look something like this:

@model MyNamespace.ViewModels.MyViewModel

@Html.EditorFor(model => model.CompName)

or @Html.TextBoxFor would work too.

Then back in your controller action on the post, you've got something like this:

[HttpPost]
public ActionResult MyAction(MyViewModel viewModel)
{
   ...
   // do whatever you want with viewModel.CompName here, like persist it back
   // to the DB
   ...
}

Might be that you use something like automapper to map your models and viewmodels but you could certainly do that manually as well, though the whole lefthand/righthand thing gets quite tedious.

Makes things much easier if you do it this way and isn't much work at all.


Update

But, if you really want to pass that value in view the ViewBag, you could do this:

In your controller action:

ViewBag.CompName = "Some Name";

Then in your view:

@Html.TextBoxFor(model =>model.Comps.CompName, new {@Value = ViewBag.CompName})

And that'll pre-populate the textbox with "Some Name".

I'd still go with the viewmodel approach, but this seems to work well enough. Hope that helps!

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your reply itsmatt. The viewbag is in my get-enter competition action and takes the name from my 'Competition' model (there is also a CompetitionEntry model(. It's so I can display the name of the competition on top of the entry form. In my competition entry model I have a navigation property 'public virtual Competition Competitions'. I thought that there would be an easy ay to pre-populate the CompetitionEntry.CompName TextBox with what's in the viewbag. I don't want to go changing my models at this stage to be honest, do you know of a way to do this in the view? Thanks again.
Dan, see my update at the end of my answer. Seems to work well enough in my test project.
I won't get to try this for an hour or so, I'll write back when I do. Thanks for all you help matt!
Hi matt, that worked, but now whe I submit the form nothing is entered into the database. when I check all fields are blank. I'll have to so some investigating..
So, I'm sure you are checking this but are the fields filled in when they get back to the controller action or are they blank there?
|
3

From your controller, if you pass a model initialized with default values using one of the View(...) method overloads that accepts a model object, these values will be used by the view when rendered. You won't need to use the @value = ... syntax.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.