2

I have edited de Create view to add a hidden field control, but i cant get the value

<div class="form-group">
   @Html.LabelFor(model => model.EmpresaId, "Empresa", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("EmpresaId", null, htmlAttributes: new { @class = "form-control", @disabled = "disabled" })
            @Html.HiddenFor(model => model.EmpresaId)
            @Html.ValidationMessageFor(model => model.EmpresaId, "", new { @class = "text-danger" })
        </div>
</div>

html created is:

    <div class="form-group">
        <label class="control-label col-md-2" for="EmpresaId">Empresa</label>
        <div class="col-md-10">
            <select class="form-control" disabled="disabled" id="EmpresaId" name="EmpresaId">
              <option selected="selected" value="1">Farmacia MZ</option>
              <option value="2">Credesa</option>
            </select>
            <input length="19" id="EmpresaId" name="EmpresaId" type="hidden" value="">
            <span class="field-validation-valid text-danger" data-valmsg-for="EmpresaId" data-valmsg-replace="true"></span>
        </div>
    </div>

As you can see hidden input has no value.

On Controller is DropDownList default value set:

public ActionResult Create()
    {
        ApplicationUser usr = db.Users.Find(User.Identity.GetUserId().ToString());
        int userId = (int)usr.EmpresaId;
        ViewBag.CategoriaId = new SelectList(db.Categorias, "CategoriaId", "Nombre");
        ViewBag.EmpresaId = new SelectList(db.Empresas, "EmpresaId", "Nombre", userId); <-- defalut value for disabled DropDownList
        ViewBag.MarcaId = new SelectList(db.Marcas, "MarcaId", "Nombre");
        return View();
    }

I've searched quite a lot but i dont understand what is the mistake. Help is wellcome !

1 Answers1

0

This HiddenFor helper clearly bounds to EmpresaId viewmodel property:

@Html.HiddenFor(model => model.EmpresaId)

The hidden field value has blank value because it not assigned from corresponding viewmodel property inside GET action method, since you're actually assigned default value of EmpresaId to ViewBag.EmpresaId which contains SelectList.

ViewBag.EmpresaId = new SelectList(db.Empresas, "EmpresaId", "Nombre", userId);

Hence you need to create a viewmodel instance, then assign default values to viewmodel properties and return to view in GET action method:

// create new instance of viewmodel and set default property value
var model = new ViewModel();
model.EmpresaId = userId;

// return default values from viewmodel to view page
return View(model);

Or simply set default value during viewmodel instance creation:

var model = new ViewModel()
{
    EmpresaId = userId;
};

return View(model);

As a side note, always use strongly-typed DropDownListFor helper instead of DropDownList to bind with viewmodel properties:

@Html.DropDownListFor(m => m.EmpresaId, ViewBag.EmpresaList as SelectList, new { @class = "form-control", @disabled = "disabled" })
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • Suggest you read [Can the ViewBag name be the same as the Model property name in a DropDownList?](https://stackoverflow.com/questions/37161202/can-the-viewbag-name-be-the-same-as-the-model-property-name-in-a-dropdownlist) to understand why the last code snippet will not work –  May 01 '18 at 02:46
  • Thanks for your suggestion, Stephen. I realized OP wrongly use a model property name as `SelectList` property in `ViewBag` and it's responsibility of OP to change it into other name. – Tetsuya Yamamoto May 01 '18 at 15:06