模型:
public class EditLeaveForm
{
[Display(Name="Employee ID")]
public string EmpID { get; set; }
public IEnumerable<SelectListItem> EmpIDs { get; set; }
[Display(Name = "Application Date")]
[DataType(DataType.Date)]
[DisplayFormat( DataFormatString="{0:yyyy-MM-dd}", ApplyFormatInEditMode=true)]
public DateTime Application_Date { get; set; }
[Display(Name = "From Date")]
[DisplayFormat( DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode=true)]
public DateTime From_Date { get; set; }
[Display(Name = "To Date")]
[DisplayFormat( DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode=true)]
public DateTime To_Date { get; set; }
[Display(Name = "Leave Type")]
public string Leave_Type { get; set; }
public IEnumerable<SelectListItem> Leave_Types { get; set; }
[Display(Name = "Reason")]
public string Reason { get; set; }
[Display(Name="Leave Sanctioned")]
public bool Sanctioned { get; set; }
public string ScriptToRun { get; set; }
public readonly int Application_ID;
[Display(Name = "Approval Date")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime Approval_Date { get; set; }
public EditLeaveForm(int application_id)
{
this.Application_ID=application_id;
this.Application_Date = DateTime.Now;
this.From_Date = DateTime.Now;
this.To_Date = DateTime.Now;
this.EmpIDs = Employee.GetAllEmpNamesAndIDs().Select(x => new SelectListItem() { Text = x.Value, Value = x.Key }).OrderBy(x => x.Text);
this.Leave_Types = Employee.GetAllLeaveTypesAvailable().Select(x => new SelectListItem() { Text = x, Value = x });
}
}
获取控制器:
[HttpGet]
public ActionResult EditLeaveApplication(int application_id)
{
EditLeaveForm applicationDetails=Employee.GetLeaveApplicationByID(application_id);
return View(applicationDetails);
}
POST 控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditLeaveApplication(EditLeaveForm formdata)
{
// process information.
}
看法:
@using (Html.BeginForm("EditLeaveApplication", "HR", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="span8 main_container edit-leave-application" style="margin:0 auto; float:none;"><h3>Edit Leave Application</h3>
@Html.HiddenFor(m => m.Application_ID)
<div class="input-append" style="display:block;">
<span class="add-on">@Html.LabelFor(m => m.EmpID, "Select Employee Name")</span>
@Html.DropDownListFor(m => m.EmpID, Model.EmpIDs, new { @class = "input-append", onchange = "EditLeaveApplication_SetEmpoloyeeName();" })
<label for="EmpName" class="add-on">Employee ID</label>
<input type="text" readonly="readonly" id="EmpName" class="add-on" size="30" />
</div>
<div class="input-append" style="display:block;">
<span class="add-on">@Html.LabelFor(m => m.Application_Date)</span>
@Html.EditorFor(m => m.Application_Date, new { @class = "add-on" })
<span class="add-on">@Html.LabelFor(m => m.Leave_Type)</span>
@Html.DropDownListFor(m => m.Leave_Type, Model.Leave_Types, new { @class = "input-append" })
</div>
<div class="input-append" >
<span class="add-on">@Html.LabelFor(m => m.From_Date)</span>
@Html.EditorFor(m => m.From_Date, new { @class = "add-on" })
<span class="add-on">@Html.LabelFor(m => m.To_Date)</span>
@Html.EditorFor(m => m.To_Date, new { @class = "add-on" })
<span class="add-on">@Html.LabelFor(m => m.Approval_Date)</span>
@Html.EditorFor(m => m.Approval_Date, new { @class = "add-on" })
</div>
<div class="well leave_reason">
<span class="input-append">@Html.LabelFor(m => m.Reason)</span>
@Html.TextAreaFor(m => m.Reason, new { @class = "add-on" })
</div>
<div class="input-append well leave_sanctioned">
<span class="add-on">@Html.LabelFor(m => m.Sanctioned)</span>
@Html.RadioButtonFor(m => m.Sanctioned, true, new { @checked = "checked", @class = "add-on" })
<span class="add-on"><label>Yes</label></span>
@Html.RadioButtonFor(m => m.Sanctioned, false, new { @class = "add-on" })
<span class="add-on"><label>No</label></span>
</div>
<div class="Action">
<input id="submit" type="submit" value="Submit" class="btn btn-primary"/>
<input id="delete" type="button" value="Delete" class="btn btn-danger"/>
<input type="button" class="btn btn-inverse" onclick="window.location.href='@Url.Action("LeaveDetails", "HR")'" id="cancel" value="Cancel" />
</div>
</div>
}
它在获取数据时工作得非常好,但它不会在表单提交时到达服务器。相反,如果我尝试访问 FormCollection 而不是 EditLeaveForm,它可以工作,但我不想使用 FormCollection。我感谢对决议的任何一点意见。请帮忙。