简体   繁体   中英

Empty POST model argument when using EditorFor

I am trying to submit a form that incorporates an @Html.EditorFor element. If I remove the EditorFor element, my POST controller argument passes data correctly, but once implemented, my entire model argument shows as null in the POST controller.

Here's the model I'm trying to pass:

public class Checkout
{
    public int CheckoutID { get; set; }
    public string Requestor { get; set; }
    public DateTime? DateRequested { get; set; }
    public List<CheckoutReceiver> Receivers { get; set; }
}

The form element on page:

@model PRI.Models.Checkout

@using (Html.BeginForm("CreateCheckout", "API/CheckoutRequest", FormMethod.Post, new { id = "pri-form" }))
{
    <div id="checkout-request">
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div id="checkout-info" class="form-horizontal">
            <div class="form-group">
                <div class="col-md-12">
                    @Html.TextBoxFor(m => m.CheckoutID)
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-12">
                    @Html.EditorFor(m => m.Receivers)
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-12">
                    <input id="create-checkout-submit" type="submit" value="Confirm transfer" class="btn btn-danger right" style="margin: 10px;" />
                </div>
            </div>
        </div>
    </div>
}

If I remove the @Html.EditorFor(m => m.Receivers) , and add data to the @Html.TextBoxFor(m => m.CheckoutID) then that passes correctly to my Post Controller, so obviously my EditorFor is messing things up:

Here's the POST controller (i put a breakpoint right after it enters this so I can check the checkout argument):

[System.Web.Http.HttpPost]
[ValidateAntiForgeryToken]
[System.Web.Http.ActionName("CreateCheckout")]
public Checkout Create(Checkout checkout)
{

    var request = new Checkout();

    return request;
}

Here's my CheckoutReceiver Editor template (removed some input elements for brevity):

@model PRI.Models.CheckoutReceiver

@using (Html.BeginCollectionItem("Receivers"))
{
<div class="form-horizontal">
    @Html.HiddenFor(model => model.ID)
    @Html.HiddenFor(model => model.CheckoutID)

    <h4 class="contact-header">@Model.ContactType</h4>

    <div class="form-group">
        <div class="col-md-5">
            <span class="form-header">Last Name</span>
            @Html.TextBoxFor(model => model.LastName, new { @class = "box-customer form-control ignore", placeholder = "Last name" })
        </div>
        <div class="col-md-5">
            <span class="form-header">First Name</span>
            @Html.TextBoxFor(model => model.FirstName, new { @class = "form-control ignore", placeholder = "First name" })
        </div>
        <div class="col-md-2">
            <span class="form-header">Middle Initial</span>
            @Html.TextBoxFor(model => model.MiddleInitial, new { @class = "form-control ignore", placeholder = "M.I." })
        </div>
    </div>
    </div>
    }

Where am I going wrong, and why is my EditorFor causing my Checkout POST argument to be null on submit?

Thanks!

Maybe you should check this question . You should add an editor for an
IEnumerable<CheckoutReceiver> instead of an editor for CheckoutReceiver .

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