Search code examples
asp.net-mvc-5.1

My Model Textbox is not Displayed properly


User id Displayed answer enter image description hereI am working on MVC 5 project in which each and every page i am getting the textbox from the model but in this page i am not able to get the textbox from the model properly....

I will explain you clearly.....

My View Page:

<div class="container">
    <fieldset>
        <legend>Leave Application</legend>
        @using (Html.BeginForm(new { @class = "form-horizontal" }))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary("", new { @style = "color:red" })
            @Html.Raw(TempData["success"])

            @* User ID *@
            <div class="form-group">
                @Html.LabelFor(m => m.UserId,new { @class = "col-md-3 control-label" })
                <div class="col-md-6">
                    @Html.TextBoxFor(m => m.UserId, new { placeholder = "Enter Userid", @readonly = "readonly", @class = "from-control" })
                    @Html.ValidationMessageFor(m => m.UserId, "", new { @style = "color:red" })
                </div>
            </div>

            @* User Name *@
            <div class="form-group">
                @Html.LabelFor(m => m.UserName, new { @class = "col-md-3 control-label" })
                <div class="col-md-6">
                    @Html.TextBoxFor(m => m.UserName, new { placeholder = "Enter UserName", @readonly = "readonly", @class = "from-control" })
                    @Html.ValidationMessageFor(m => m.UserName, "", new { @style = "color:red" })
                </div>
            </div>

            @* Leaves Required *@
            <div class="form-group">
                @Html.LabelFor(m => m.LeavesAvailed, new { @class = "col-md-3 control-label" })
                <div class="col-md-6">
                    @Html.TextBoxFor(m => m.LeavesAvailed, new { placeholder = "Enter Userid", @readonly = "readonly", @class = "from-control" })
                    @Html.ValidationMessageFor(m => m.LeavesAvailed, "", new { @style = "color:red" })
                </div>
            </div>

            @* Nature of Leave *@
            <div class="form-group">
                @Html.LabelFor(m => m.NatureofLeave, new { @class = "col-md-3 control-label" })
                <div class="col-md-6">
                    @Html.DropDownListFor(m => m.NatureofLeave, ViewBag.AssignRoleViewModel as IEnumerable<SelectListItem>, "-Select-")
                    @Html.ValidationMessageFor(m => m.NatureofLeave, "", new { @style = "color:red" })
                </div>
            </div>

            @* Start Date *@
            <div class="form-group">
                @Html.LabelFor(m => m.StartDate, new { @class = "col-md-3 control-label" })
                <div class="col-md-6">
                    @Html.TextBoxFor(m => m.StartDate, new { placeholder = "Enter Userid", @readonly = "readonly", @class = "from-control" })
                    @Html.ValidationMessageFor(m => m.StartDate, "", new { @style = "color:red" })
                </div>
            </div>

            @* End Date *@
            <div class="form-group">
                @Html.LabelFor(m => m.EndDate, new { @class = "col-md-3 control-label" })
                <div class="col-md-6">
                    @Html.TextBoxFor(m => m.EndDate, new { placeholder = "Enter Userid", @readonly = "readonly", @class = "from-control" })
                    @Html.ValidationMessageFor(m => m.EndDate, "", new { @style = "color:red" })
                </div>
            </div>

            @* Reason *@
            <div class="form-group">
                @Html.LabelFor(m => m.Reason, new { @class = "col-md-3 control-label" })
                <div class="col-md-6">
                    @Html.TextBoxFor(m => m.Reason, new { placeholder = "Enter Userid", @readonly = "readonly", @class = "from-control" })
                    @Html.ValidationMessageFor(m => m.Reason, "", new { @style = "color:red" })
                </div>
            </div>
            <ul>
                @* Userid *@
                <li>
                    @Html.LabelFor(m => m.UserId)
                    @Html.TextBoxFor(m => m.UserId, new { placeholder = "Enter Userid", @readonly = "readonly" })
                    @Html.ValidationMessageFor(m => m.UserId, "", new { @style = "color:red" })
                </li>
                @* Username *@
                <li>
                    @Html.LabelFor(m => m.UserName)
                    @Html.TextBoxFor(m => m.UserName, new { placeholder = "Enter Username", @readonly = "readonly" })
                    @Html.ValidationMessageFor(m => m.UserName, "", new { @style = "color:red" })
                </li>
                @* Leaves Availed *@
                <li>
                    @Html.LabelFor(m => m.LeavesAvailed)
                    @Html.TextBoxFor(m => m.LeavesAvailed, new { placeholder = "Number of Days Required" })
                    @Html.ValidationMessageFor(m => m.LeavesAvailed, "", new { @style = "color:red" })
                </li>
                @* Type of Leave *@
                <li>
                    @Html.LabelFor(m => m.NatureofLeave)
                    @Html.DropDownListFor(m => m.NatureofLeave, ViewBag.AssignRoleViewModel as IEnumerable<SelectListItem>, "-Select-")
                    @Html.ValidationMessageFor(m => m.NatureofLeave, "", new { @style = "color:red" })
                </li>
                @* Start Date *@
                <li>
                    @Html.LabelFor(m => m.StartDate)
                    @Html.TextBoxFor(m => m.StartDate, new { placeholder = "Enter Startdate" })
                    @Html.ValidationMessageFor(m => m.StartDate, "", new { @style = "color:red" })
                </li>
                @* End Date *@
                <li>
                    @Html.LabelFor(m => m.EndDate)
                    @Html.TextBoxFor(m => m.EndDate, new { placeholder = "Enter Enddate", @readonly = "readonly" })
                    @Html.ValidationMessageFor(m => m.EndDate)
                </li>
                @* Reason *@
                <li>
                    @Html.LabelFor(m => m.Reason)
                    @Html.TextBoxFor(m => m.Reason, new { placeholder = "Enter Reason for Leave" })
                    @Html.ValidationMessageFor(m => m.Reason, "", new { @style = "color:red" })
                </li>
            </ul>
            <div class="btn-toolbar">
                <button type="submit" id="btnLeaves" name="btnLeaves" class="btn btn-success btn-pressure">Apply</button>
                <button type="button" id="btnCancel" name="btnCancel" class="btn btn-primary btn-pressure">Cancel</button>
            </div>
        }
    </fieldset>
</div>

My Model:

public class EmployeeLeave
    {
        //[Display(Name = "User Id")]
        public int UserId { get; set; }

        [Display(Name = "Leave Id")]
        public int NatureId { get; set; }

        [Display(Name = "User Name")]
        public string UserName { get; set; }

        [Display(Name = "Type of Leave")]
        public List<SelectListItem> NatureofLeave { get; set; }

        //[DataType(DataType.DateTime)]
        //[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yy}")]
        public DateTime StartDate { get; set; }

        //[DataType(DataType.DateTime)]
        //[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yy}")]
        public DateTime EndDate { get; set; }

        [Required]
        [Display(Name = "Reason")]
        public string Reason { get; set; }

        [Required]
        [Display(Name = "No. of Leaves Required")]
        public int LeavesAvailed { get; set; }

        //public int LeavesRemained { get; set; }

        //public int TotalLeaves { get; set; }
    }

My Controller:

[HttpGet]
        [AllowAnonymous]
        public ActionResult Leave()
        {
            EmployeeLeave objEmpLeave = new EmployeeLeave();
            objEmpLeave.UserId = Convert.ToInt32(Session["userId"]);
            objEmpLeave.UserName = Convert.ToString(Session["name"]);
            objEmpLeave.NatureofLeave = BindDataFromDatabaseToLeave();
            return View(objEmpLeave);
        }
[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Leave(EmployeeLeave objEmpLeave)
        {
            objEmpLeave.NatureofLeave = BindDataFromDatabaseToLeave();
            var selectitem = objEmpLeave.NatureofLeave.Find(p => p.Value == objEmpLeave.NatureId.ToString());

            if (selectitem != null)
            {
                selectitem.Selected = true;
                ViewBag.Message = "NatureofLeave " + selectitem.Text;
            }
            if (string.IsNullOrEmpty(objEmpLeave.StartDate.ToString()))
            {
                ModelState.AddModelError("Error", "Please provide start date");
            }
            else if (string.IsNullOrEmpty(objEmpLeave.EndDate.ToString()))
            {
                ModelState.AddModelError("Error", "Please provide end date");
            }
            else if (string.IsNullOrEmpty(objEmpLeave.NatureofLeave.ToString()))
            {
                ModelState.AddModelError("Error", "Select one");
            }
            else if (string.IsNullOrEmpty(objEmpLeave.Reason))
            {
                ModelState.AddModelError("Error", "Reason for the Leave");
            }

            else
            {
                //objEmpLeave.UserId = Convert.ToInt32(Session["userId"]);
                //objEmpLeave.UserName = Convert.ToString(Session["name"]);
                int leaveID = objIAccountData.InsertLeave(objEmpLeave.UserId);
                int numberofleavesavailed = objEmpLeave.LeavesAvailed;

            }
            return View(objEmpLeave);
        }

Finally Output is: User Id

but i require textbox beside UserId label Here User Id is a label The remaining pages works fine for me Thanks in advance if you can help me alot.... I am struggling for 2 days


Solution

  • use this please:

        <div class="container">
    <fieldset>
        <legend>Leave Application</legend>
        @using (Html.BeginForm(new { @class = "form-horizontal" }))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary("", new { @style = "color:red" })
            @Html.Raw(TempData["success"])
    
            @* User ID *@
            <div class="form-group">
                @Html.LabelFor(m => m.UserId,new { @class = "col-md-3 control-label" })
                <div class="col-md-6">
                    @Html.TextBoxFor(m => m.UserId, new { placeholder = "Enter Userid", @readonly = "readonly", @class = "from-control" })
                    @Html.ValidationMessageFor(m => m.UserId, "", new { @style = "color:red" })
                </div>
            </div>
    
            @* User Name *@
            <div class="form-group">
                @Html.LabelFor(m => m.UserName, new { @class = "col-md-3 control-label" })
                <div class="col-md-6">
                    @Html.TextBoxFor(m => m.UserName, new { placeholder = "Enter UserName", @readonly = "readonly", @class = "from-control" })
                    @Html.ValidationMessageFor(m => m.UserName, "", new { @style = "color:red" })
                </div>
            </div>
    
            @* Leaves Required *@
            <div class="form-group">
                @Html.LabelFor(m => m.LeavesAvailed, new { @class = "col-md-3 control-label" })
                <div class="col-md-6">
                    @Html.TextBoxFor(m => m.LeavesAvailed, new { placeholder = "Enter Userid", @readonly = "readonly", @class = "from-control" })
                    @Html.ValidationMessageFor(m => m.LeavesAvailed, "", new { @style = "color:red" })
                </div>
            </div>
    
            @* Nature of Leave *@
            <div class="form-group">
                @Html.LabelFor(m => m.NatureofLeave, new { @class = "col-md-3 control-label" })
                <div class="col-md-6">
                    @Html.DropDownListFor(m => m.NatureofLeave, ViewBag.AssignRoleViewModel as IEnumerable<SelectListItem>, "-Select-")
                    @Html.ValidationMessageFor(m => m.NatureofLeave, "", new { @style = "color:red" })
                </div>
            </div>
    
            @* Start Date *@
            <div class="form-group">
                @Html.LabelFor(m => m.StartDate, new { @class = "col-md-3 control-label" })
                <div class="col-md-6">
                    @Html.TextBoxFor(m => m.StartDate, new { placeholder = "Enter Userid", @readonly = "readonly", @class = "from-control" })
                    @Html.ValidationMessageFor(m => m.StartDate, "", new { @style = "color:red" })
                </div>
            </div>
    
            @* End Date *@
            <div class="form-group">
                @Html.LabelFor(m => m.EndDate, new { @class = "col-md-3 control-label" })
                <div class="col-md-6">
                    @Html.TextBoxFor(m => m.EndDate, new { placeholder = "Enter Userid", @readonly = "readonly", @class = "from-control" })
                    @Html.ValidationMessageFor(m => m.EndDate, "", new { @style = "color:red" })
                </div>
            </div>
    
            @* Reason *@
            <div class="form-group">
                @Html.LabelFor(m => m.Reason, new { @class = "col-md-3 control-label" })
                <div class="col-md-6">
                    @Html.TextBoxFor(m => m.Reason, new { placeholder = "Enter Userid", @readonly = "readonly", @class = "from-control" })
                    @Html.ValidationMessageFor(m => m.Reason, "", new { @style = "color:red" })
                </div>
            </div>
            <div class="btn-toolbar">
                <button type="submit" id="btnLeaves" name="btnLeaves" class="btn btn-success btn-pressure">Apply</button>
                <button type="button" id="btnCancel" name="btnCancel" class="btn btn-primary btn-pressure">Cancel</button>
            </div>
        }
    </fieldset>
    

    with this css:

        form ul{ background: #eee; margin: 0.1em; border: 2px solid red; } 
        form li{ border: 2px solid #eee; border-bottom: 0; margin-bottom: 0; 
        position: relative; } 
        form li:first-child { border-top: 0; } 
        form li:nth-child(2) { border: 2px solid #eee; }
        label, input, textarea { display: block; border: 0; } 
        input, textarea { width: 100%; height: 100%; padding: 2.25em 1.2em 1em; 
        outline: 0; border: 2px solid #eee; } 
        label { font-size: 1em; position: absolute; top: 1em; left: 1.15em; 
        color: #000000; opacity: 1; }
    

    Remove the ul element and its content and make sure that the text is not white. If text is white and background is also white then it will ofcourse not be seen.