0

I am trying to create a user interface in my ASP.NET MVC application for creating plans in stripe. Stripe requires a product ID in order to create a subscription plan, so I need to display a dropdown that lists the available products to the customer.

I have an option where the customer can create products from a form, and this works perfectly, however, I am having trouble understanding how to pass this into Html.DropDownList("ProductId"). I first thought that I would need to save duplicate data in order to achieve this, however, when I tried to do this, I received an error saying that my model has a value of null, because in my method I am creating two products - one in the api, and one locally - I have changed this back to only use the API.

I've tried more than just this, but this is most recent:

public ActionResult Create()
{
    StripeList<Product> products = new StripeList<Product>();
    ViewBag.Products = new SelectList(products, "Id", "Name");
    return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(PlanCreateViewModel model)
{
    if (ModelState.IsValid)
    {
        var options = new PlanCreateOptions
        {
            Id = model.Id,
            Amount = model.Amount,
            Currency = model.Currency.ToLower(),
            Interval = model.Interval,
            Product = model.Product,
            IntervalCount = model.IntervalCount,
        };

        var planService = new PlanService();
        await planService.CreateAsync(options);
        return RedirectToAction("Index");
    }
    return View(model);
}

// html helper returns null reference exception 
@Html.DropDownList("Products", null, new { @class = "form-control" })

TL;DR: How can I retrieve a list of the stripe products that display the name of the product, but have the value of the product ID in asp.net mvc5.

1 Answer 1

0

Upon reviewing the Stripe API, I have managed to list all the products in the dropdown by using the ProductService() method. Here is the result:

[HttpGet]
public ActionResult Create()
{
    var service = new ProductService();
    var options = new ProductListOptions { Limit = 5, };
    StripeList<Product> products = service.List(options);
    ViewBag.Products = new SelectList(products, "Id", "Name");
    return View();
}
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.