0

I have a View that displays a "grid" of all the Payments, which works fine.

Now, I would like to also add an additional dropdown to that View with the list of the Merchants.

How should I go about it? Add another field in my PaymentViewModel to have a list of the Merchants? Create two models? Can I create a partial view for just the dropdownlist and bind it another model?

I'm new to MVC so any thoughts are welcome.

Payments.cshtml

@model IEnumerable<MVC.Web.Models.PaymentsViewModel>

PaymentsViewModel.cs

public int PaymentID { get; set; }
public string Merchant { get; set; }
1
  • 1
    It depends. If you intend on having both values posted back together as a single model and you're using the default posting, then a single model works best. If you want to post with a javascript AJAX call, you could pass values in as multiple parameters easily enough. In this case, though, you can use @Html.DropDownListFor(x=> x.Merchant ...); throughout the app without creating separate views. Commented Oct 10, 2013 at 22:38

1 Answer 1

2

If you want one dropdown for the entire view, then you wouldn't add the list to PaymentsViewModel, as that would be adding a dropdown for each payment. Also this viewmodel should have a singular name because each represents a single item: PaymentViewModel.

Payments.cshtml

@model PaymentsViewModel

@Html.DropDownFor(... Model.Merchants...)// select into ListItems

<table>...
foreach(PaymentViewModel payment in Model.Payments)
    ...your existing "grid"

PaymentViewModel.cs

public int PaymentID { get; set; }
public string Merchant { get; set; }

PaymentsViewModel.cs

List<MerchantsViewModel> Merchants {get;set;}
List<PaymentViewModel> Payments {get;set;}

Alot of how I would go about this depends on what you are trying to accomplish with this dropdown though.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.