3

Currently i have this field on a form:

@Html.EditorFor(model => model.AmountDecimal, 
  new { htmlAttributes = new { @class = "form-control" } })

But:

a) i want it to have the value "100" predefined

b) don't want it to be editable

I know how to do it in raw HTML but i need it to be in razor.

Thanks

1
  • 1
    The value will be taken from the Model's AmountDecimal, you can change it there. The disabled attribute you can add to the same anonymous object that already contains the class. Commented Sep 18, 2016 at 16:35

2 Answers 2

3

It would make sense to set this value e.g. in the constructor of your model or in the controller before you call your view

public ActionResult MyAction()
{
    var model = new MyViewModel
    {
        AmountDecimal= 100
    };
    return View(model);
}

But if you really like to do it in razor, you may use HiddenFor

@Html.HiddenFor(model => model.AmountDecimal, new { @Value = "100" });
<input type="text" name = "dummy" class="form-control" value="100" readonly/>

Keep in mind to never trust a user input ;)

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

Comments

1

I think your are loooking for something like this:

@Html.EditorFor(model => model.AmountDecimal, 
  new { htmlAttributes = new { @class = "form-control", @Value = "100", @readonly = "readonly"} })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.