简体   繁体   中英

Model is not passed back from View to Controller in ASP.NET MVC3

Model:

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

}

Get Controller:

 [HttpGet]     
 public ActionResult EditLeaveApplication(int application_id)
 {
    EditLeaveForm applicationDetails=Employee.GetLeaveApplicationByID(application_id);            
    return View(applicationDetails);
 }

POST Controller:

    [HttpPost]        
    [ValidateAntiForgeryToken]
    public ActionResult EditLeaveApplication(EditLeaveForm formdata)
    {
        // process information.
    }

View:

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

}

It works absolutely fine while getting the data, but it doesn't hit the server on form submit. Instead, if I'm trying to access FormCollection rather than EditLeaveForm, it works, but I d not want to use FormCollection . I appreciate any little input towards the resolution. Please help.

In order for the model binder to work you need to have the default constructor defined:

So add this to your class and you wont have to use FormCollection

public class EditLeaveForm
{
    public EditLeaveForm() { }
    public EditLeaveForm(int application_id) : base()
    {
        //...
    }
    //...
}

Also make sure that all the properties you want to bind to have getters and setters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM