Use hard code in MVC3 Razor view engine

advertisements

I have the following Razor code:

<td>
    <div class="editor-label">
         @Html.LabelFor(model => model.AccountCode)
    </div>
</td>
<td>
    <div class="editor-field">
         @Html.EditorFor(model => model.AccountCode)
    </div>
</td>

I would like to hard code the value 1001 as a default for the Input field. I have tried [model.accountcode ="1001"] which does not work.

Please explain with example code.


If you want to set a value in the model, that should be done in the controller...

public ActionResult MyAction()
{
    var myModel = new ModelClass();
    myModel.AccountCode = 1001;
    return View("MyViewName", myModel);
}

Then use the normal syntax in Razor:

@Html.TextBoxFor(m => m.AccountCode)

Or if you just want a hidden parameter:

@Html.HiddenFor(m => m.AccountCode)