我的模型文本框未正确显示

My Model Textbox is not Displayed properly

User id Displayed answer enter image description here我正在处理 MVC 5 项目,其中每一页都从模型中获取文本框,但在此页面中我无法从模型中正确获取文本框....

我给你解释清楚.....

我的浏览页面:

<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>

我的模特:

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; }
    }

我的控制器:

[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);
        }

最终输出为: 用户 ID

但我需要 UserId 标签旁边的文本框 这里User Id是一个标签 其余页面对我来说很好用 如果您能帮我很多,请提前致谢.... 我挣扎了2天

请使用这个:

    <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>

用这个 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; }

删除 ul 元素及其内容,并确保文本不是白色的。如果文字是白色的,背景也是白色的,那当然看不到了。