型号:
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 。我很欣赏对该决议的任何一点意见。请帮忙。
答案 0 :(得分:0)
为了使模型绑定器工作,您需要定义默认构造函数:
因此,请将此添加到您的课程中,您不必使用FormCollection
public class EditLeaveForm
{
public EditLeaveForm() { }
public EditLeaveForm(int application_id) : base()
{
//...
}
//...
}
还要确保要绑定的所有属性都有getter和setter。