0

I'm using Asp.Net MVC 5 with EF 6.

I have a Model, let's call it "Checkout". It has the following properties:

int BuyerId
List<CheckoutItem> Items

CheckoutItem has the following properties:

int ItemId
decimal Price
bool IsPaid

I want to have a view first display EditorFor -> BuyerId. When the user enters a BuyerId, the view should update with the Checkout data, the items displayed in a list in which the user can click a "remove" link to remove that item from the current transaction (the View's current Model). When the user clicks on the submit button, I want the controller to update the items in the Checkout.Items list with IsPaid = true.

I understand all of the logic needed for all of this. The problem I'm having is how this all works withing the MVC context.

Currently I have Checkout.cshtml whose Model is Buyer (accessed by BuyerId) which has a textbox for the user to enter the BuyerId and an empty <div>. Using jquery on .focusout I use .ajax to call the controller and return a partial view, "_CheckoutDetail". _CheckoutDetail uses Checkout Model to display the information in the model.

Currently _CheckoutDetail has a form, but when I submit it from the partial view, the binding isn't working.

If this is all clear what I'm trying to do, my question is, what is the best way to accomplish this? I expected my partial view to post its Model back to the controller, but it isn't working. I've tried both

public async Task<ActionResult> CheckoutDetails(CheckoutViewModel checkout)

and

public async Task<ActionResult> CheckoutDetails([Bind(Include = "BuyerId,Items")] CheckoutViewModel checkout)

in my controller definition. In both cases I receive a message "The name 'checkout' does not exist in the current context".

Thank you for your help.

John
  • 313
  • 1
  • 4
  • 8
  • Check this out: https://stackoverflow.com/questions/8002059/asp-net-mvc-display-template-for-a-collection it might help lead you in a more productive direction – Pseudonym Apr 28 '15 at 18:40
  • Maybe I'm not following. I really haven't had any trouble displaying the information (although, my approach may also be an issue). What I'm struggling to accomplish is to get the Items list to have a command which removes itself from the list and having the ability to post the updated Model back to the controller. – John Apr 28 '15 at 19:09
  • You need to show your view for `CheckoutViewModel`. If binding is not working, its almost always because the view is not generating the correct name attributes for the controls. You also need to show the model for `CheckoutViewModel` –  Apr 28 '15 at 23:47

0 Answers0