简体   繁体   中英

MVC default model binder returns Null object

I am having an issue where the default model binder is refusing to bind to my object, which is a List<> of simple objects.

The class:

public class ReferralHistoryDetail {

    [Key]
    public long Referral_Number { get; set; }
    public Guid QuoteGuid { get; set; }
    public byte ReferralTypeID { get; set; }
    public string Referral_Type { get; set; }
    public DateTime ReferralDateTime { get; set; }
    public string ReferralComments { get; set; }

}

The controller definition:

[HttpPost]
public ActionResult Save(List<ReferralHistoryDetail> details) {

The view from Fiddler:

提琴手的观点

What am I doing wrong here?

This is shown via an EditorTemplate in the following manner:

@using (Html.BeginForm("Save", "Home")) {

    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <div class="tab-content">
        <div class="tab-pane fade in active form-horizontal" id="basic">
            @Html.EditorFor(m => m.ReferralHistoryDetail)
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>

}

The value that is coming across as NULL is the one being passed to the POST method of the controller. The default model binder is not able to bind this.

I resolved this.

The problem is the Html.BeginForm() is in the main view, but it is only posting back stuff from the EditorFor() 's.

I had to change my controller to take in the main view's object and not the object that the EditorFor() is dealing with, because the Html.BeginForm() lives on the main view.

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