Display Model Property or ViewBag in ReadOnly TextBox in ASP.Net MVC

Last Reply 5 months ago By dharmendr

Posted 5 months ago

Hello every1

How to displaying a property or viewbag in readonly textbox mvc 5. 

I am extremely frustrated right now this should be very easy to do and I have tried SO MANY different ways to do this but for some reason I cannot seem to display what I need to. I have been working on this for hours now and usually its just

@if (ViewBag.NextPartNumber != null) {

    @ViewBag.NextPartNumber

}

Or

@Html.EditorFor(x => x.PartNextNumber, new { htmlAttributes = new { @class="Form-control", @Value = Model.PartNextNumber }})

I Select an option in a Dropdownlist and then when the option is selected it displays accordingly.

Not sure why these are not working but I have a Model in my view and the property is set in Debug correctly when I pass it to the view. I also tried setting the Viewbag instead ether way I would be happy with. Here is my code a bunch of what I been trying whatever way to display it id be happy with.

        // Controller(If you want my method showing how I get this I can add it but correct here)              
        public ActionResult TakeOutPart()
        {
            CreatePartBookSelectList();
            CreateEnteredBySelectList();
            CreateSoftwareTypeSelectList();
            CreateSourceSelectList();
            CreateManufacturerSelectList();
            CreateDescriptionSelectList();
            CreateUsageSelectList();
            CreateUnitsSelectList();
           // CreateMachineTypeSelectList();
            var model = new PartNumberViewModel();
            model.ListMachTypes = GetMachineTypes();

            return View(model);
        }
        //[HttpPost, ActionName("TakeOutPart")]
        public ActionResult TakeOutPartVM(PartNumberViewModel model)
        {
            CreatePartBookSelectList();
            CreateEnteredBySelectList();
            CreateSoftwareTypeSelectList();
            CreateSourceSelectList();
            CreateManufacturerSelectList();
            CreateDescriptionSelectList();
            CreateUsageSelectList();
            CreateUnitsSelectList();

            model.ListMachTypes = GetMachineTypes();
            ViewBag.NextPartNumber = model.PartNextNumber;

            return View("TakeOutPart", model);
        }

 

<div class="col-md-3 col-lg-3">
    @Html.EditorFor(x => x.PartNextNumber, new { htmlAttributes = new { @class="Form-control", @Value = Model.PartNextNumber }})
    <br />
    @Html.TextBox("lPartNextNumber", DateTime.Now.ToString() )
    @Html.TextBox("lPartNextNumber", (string) @ViewBag.NextPartNumber )
    <br />
    @Html.TextBoxFor(c=>c.PartNextNumber)
    <br /><br />
    @Html.EditorFor(x => x.PartNextNumber, new { htmlAttributes = new { @class="Form-control", @readonly="readonly" }})
    <p>(string) @ViewBag.NextPartNumber</p>
</div>