@Html.LabelFor(model => model.Number)
@Html.Hidd" /> @Html.LabelFor(model => model.Number)
@Html.Hidd" /> @Html.LabelFor(model => model.Number)
@Html.Hidd"/>

TextBoxFor not displaying value (MVC4)

788 Views Asked by At

I got the following controls in my view:

<div class="editor-label">
@Html.LabelFor(model => model.Number)
</div>
<div class="editor-field">
@Html.HiddenFor(model => model.Number)
@Html.TextBoxFor(model => model.Number, new { disabled = "disabled" })
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProjectID)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Project.ID, Model.ProjectList, "Select", new { onchange = "SelectedIndexChanged()" })
</div>

I submit the form when the dropdownlist value change to generate the Number field of the model, then the view is reloaded. When the is no HiddenFor control, the Number of the model is displayed properly in the TextBoxFor. When I add a HiddenFor control, the Number field is not displayed although Model.Number is not null.

What is the reason of this?

The reason why I want a HiddenFor with the Number value is that when I submit the form for saving, model.Number equals null. I thought by putting the value in a HiddenFor I would have access to it...

Edit: Controller method

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CreateOpportunity(CreateEditOpportunityModel model)
    {
        // if the submit is made by the ddl
        if (model.SubmitValue.Equals("ChangeProject"))
        {
            Project parentProject = pCtx.Projects.Find(model.Project.ID);

            if (parentProject != null)
            {   
                // Generate the "Number field"
                Region projectRegion = rCtx.Regions.Find(parentProject.RegionID);
                int numOpportunity = oCtx.Opportunitites.Count(o => o.ProjectID == parentProject.ID);
                numOpportunity++;
                string oppNumber = numOpportunity < 10 ? "0" + numOpportunity : numOpportunity.ToString();
                model.Number = projectRegion.Name + "-" + parentProject.ID + "-" + oppNumber;



            }

            // Repopulate the ddl
            model.ProjectList = new SelectList(pCtx.Projects, "ID", "ID");

            return View(model);
        }

        // if submit is made by the save button
        else if (model.SubmitValue.Equals("CreateOpportunity"))
        {
            if (ModelState.IsValid)
            {
                Opportunity opportunity = new Opportunity();
                opportunity.ID = model.ID;
                opportunity.Number = model.Number;
                opportunity.ActivationDate = model.ActivationDate;
                opportunity.Assignee = model.Assignee;
                opportunity.Comments = model.Comments;
                opportunity.CreationDate = DateTime.Now;
                opportunity.LicenseFilePath = model.LicenseFilePath;
                opportunity.LicenseRequestFilePath = model.LicenseRequestFilePath;
                opportunity.OkPermanentStatus = model.OkPermanentStatus;
                opportunity.PaidStatus = model.PaidStatus;
                opportunity.PartNumber = model.PartNumber;
                opportunity.Project = pCtx.Projects.Find(model.Project.ID);

                //tell EF that Project already exists in Company table
                oCtx.Entry(opportunity.Project).State = EntityState.Modified;

                // Saves project
                oCtx.Opportunitites.Add(opportunity);
                oCtx.SaveChanges();
                return RedirectToAction("Index");
            }

            model.ProjectList = new SelectList(pCtx.Projects, "ID", "ID");

            return View(model);
        }

        model.ProjectList = new SelectList(pCtx.Projects, "ID", "ID");

        return View(model);
    }

This part of the code works fine, I'm just putting it here to clarify things.

0

There are 0 best solutions below