3

I have a view consisting of a form and textboxes. How can I set a default value in each box for strings and int values?

I want the page to load up each box's value so I don't need to type values.

I'm not able to change anything in the Model.

@model MyDb.Production.ProductionMaterial

@{
  ViewBag.Title = "CreateMaterial";
 }


@using (Html.BeginForm()) {
  @Html.ValidationSummary(true)
  <fieldset>
    <legend>ProductionOrderMaterial</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Position)
    </div>
    <div class="editor-field">                   //something like
        @Html.TextBoxFor(model => model.Position, Or 5 )
        @Html.ValidationMessageFor(model => model.Position)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ArticleId)
    </div>
    <div class="editor-field">                  //something like
        @Html.TextBoxFor(model => model.ArticleId. Or "")
        @Html.ValidationMessageFor(model => model.ArticleId)
    </div>


}
Michael
  • 8,362
  • 6
  • 61
  • 88
Fadi Alkadi
  • 781
  • 4
  • 12
  • 22

2 Answers2

8

Create an instance of the model in your action, assign some values to the properties of the model and pass that into the View method.

In your action have:

ProductionMaterial model = new ProductionMaterial();
model.Position = 5;
return this.View(model);

This will pass the model to the view and TextBoxFor( model => model.Position ) will display 5.

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
MotoSV
  • 2,348
  • 17
  • 27
  • you mean that i put the vale in viewbag and send them to – Fadi Alkadi May 18 '13 at 14:45
  • You can ignore the view bag. The TextBoxFor references model.Position which then references the model defined at the top of the "model ProductionMaterial". The property Position will be used to populate the TextBoxFor. (I've updated my answer) – MotoSV May 18 '13 at 14:51
  • i dont exactly understand u, what should i write in my action ? – Fadi Alkadi May 18 '13 at 14:54
  • I've updated my answer. Your view binds the controls to properties on your model. If you want default values when the view loads, then use the controller action to create the model, decide what default values should be assigned to the model, then pass the model to the view. – MotoSV May 18 '13 at 15:03
  • Thanks it works :) it was enough to create a new instans of the model and send it to the view – Fadi Alkadi May 18 '13 at 16:16
7

I see you already got answer, but I'll show you how you can do it another way:

In Controller

 public ActionResult Index()
            {
                //here you must set VALUE what u want,
                //for example I set current date
                Viewbag.ExactlyWhatYouNeed = DateTime.Now 


                return View();
            }

In View

  @Html.TextBoxFor(model => model.CurrentDate, new { @Value = ViewBag.ExactlyWhatYouNeed})

And when you will load your View, you will get field with default value (current date in our example)

Its work on MVC 4

Hope Its will be usefull info for another people.

Nazarii Iaremii
  • 133
  • 3
  • 8