Asp.net mvc 5.1 “我的模型”文本框未正确显示
我正在MVC5项目中工作,在该项目中,我从模型中获取每个页面的文本框,但在该页面中,我无法从模型中正确获取文本框 我会给你解释清楚 我的查看页面:Asp.net mvc 5.1 “我的模型”文本框未正确显示,asp.net-mvc-5.1,Asp.net Mvc 5.1,我正在MVC5项目中工作,在该项目中,我从模型中获取每个页面的文本框,但在该页面中,我无法从模型中正确获取文本框 我会给你解释清楚 我的查看页面: <div class="container"> <fieldset> <legend>Leave Application</legend> @using (Html.BeginForm(new { @class = "form-horizontal" }))
<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>
最后输出为:
用户Id
但我需要用户ID标签旁边的文本框
这里的用户Id是一个标签
剩下的几页对我来说很好
如果你能帮我很多忙,请提前谢谢。。。。
我挣扎了两天请使用这个:
<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>
删除ul元素及其内容,并确保文本不是白色。如果文本是白色的,背景也是白色的,那么它当然不会被看到。hi,所以在前端你只能看到UserId标签,或者你也可以看到值??对于exmaple用户Id:1您好,先生,我看到了用户Id标签以及编号1…..对不起,当我单击标签(即用户Id)时,会显示编号。它是如何显示的?你能做个截图吗。我不能很好地理解你,先生……我不知道你想做什么。但你没有回答我的问题。您是否看到前端中的值,例如Username:TestUser?它是如何显示的?是的,先生,我在前端看到用户名或用户ID的答案,但当我单击标签用户ID时,只显示答案用户ID值是白色的,我想是的。。。。。我会为这篇文章贴一个帖子,也请等一下,sirok。但为什么它是这样的,只是点击?以及为什么要呈现模型2次:一个是div:Html.TextBoxFor(m=>m.UserId,new{placeholder=“Enter UserId”,另一个是ul:
- @Html.LabelFor(m=>m.UserId)@Html.TextBoxFor(m=>m.UserId,new{placeholder=“Enter UserId”,“readonly=“readonly”)我把照片贴上去了,先生,看一看
[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); }
<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>
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; }