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标签旁边的文本框
这里用户Id是一个标签
剩下的几页对我来说很好
我挣扎了两天