model binding is not working

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 năm cách đây
My model binding is not working...
If I debug my controller:
          public ActionResult Edit(ShippingCustomerAccountModel model, int id, FormCollection form)
the model properties are null, yet the form (collection) has the values.

My Edit.cshtml has this code which is like that in core's \Views\CustomerAddressEdit.cshtml
@{
    var dataDict = new ViewDataDictionary();
    //Merge ModelState (required for validation)
    dataDict.ModelState.Merge(ViewData.ModelState);
    dataDict.TemplateInfo.HtmlFieldPrefix = "Account";
    @Html.Partial("~/Plugins/MyPlugin/Views/CustomerAccounts/_CreateOrUpdate.cshtml", Model.CustomerAccount, dataDict)
}


My _CreateOrUpdate has:

    <div class="inputs">
        @Html.LabelFor(model => model.AccountNumber, new { }, ":")
            @Html.EditorFor(model => model.AccountNumber)
        @Html.RequiredHint()
        @Html.ValidationMessageFor(model => model.AccountNumber)
    </div>


This is the emitted html in browser:
<div class="inputs">
    <input class="text-box single-line" id="Account_AccountNumber" name="Account.AccountNumber" type="text" value="">

    <span class="field-validation-valid" data-valmsg-for="Account.AccountNumber" data-valmsg-replace="true"></span>
</div>



Am I missing something about how TemplateInfo.HtmlFieldPrefix works?
7 năm cách đây
New York wrote:
Am I missing something about how TemplateInfo.HtmlFieldPrefix works?

I think you might just need to change the HtmlFieldPrefix to CustomerAccount:

@{
    var dataDict = new ViewDataDictionary();
    //Merge ModelState (required for validation)
    dataDict.ModelState.Merge(ViewData.ModelState);
    dataDict.TemplateInfo.HtmlFieldPrefix = "CustomerAccount";
    @Html.Partial("~/Plugins/MyPlugin/Views/CustomerAccounts/_CreateOrUpdate.cshtml", Model.CustomerAccount, dataDict)
}

I'm assuming you've got a complex property on your ShippingCustomerAccountModel called CustomerAccount which has an AccountNumber property.
7 năm cách đây
That was it!  Thanks.

(any thoughts about my 'labelFor' issue?)
7 năm cách đây
New York wrote:
That was it!  Thanks.

No problem, but at the risk of teaching my grandmother to suck eggs, have you considered if you need to use this approach? The reason that nop uses this approach on the address editing is because there are multiple places in the front end that a customer can edit an address (from their account, the delivery address and shipping address during checkout, maybe some others).

Breaking out CreateOrUpdateAddress.html into a separate shared view that takes in an AddressModel allows nop to reuse that view anywhere that addresses can be edited (but at the expense of having to handle the ModelState merging manually). This Code Project article gives a really good explanation of the approach that nop uses (and what the alternatives are) but unless you actually need the code reuse aspect then it would be much easier to bind directly to the ShippingCustomerAccountModel in your Edit view with something like:

@Html.EditorFor(model => model.CustomerAccount.AccountNumber)

Anyway, apologies if you know all this and have chosen this approach for exactly that reason but I figured it doesn't do any harm to have the explanation on the forum (even if it's only so that I can refer back to it at some point in the future).

New York wrote:
(any thoughts about my 'labelFor' issue?)

I saw it but haven't replied as I don't have any particular insight beyond what you can find on Stack Overflow by googling the error message. I didn't see anything definitive but most of it points towards some sort of mismatch between assembly version numbers in References and the plugin's web.config, so that's where I'd start looking.
7 năm cách đây
petemitch wrote:
... have you considered if you need to use this approach?...

Yes, I plan on using it in two places.  Thanks for all the links; good stuff to know :)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.