Pregunta

I'm using MVC 5, and have a page where I will list out payments for a contract. there are times when there are no payments associated with a contract. my view brings in the model to list:

@model List<MyProject.Models.PaymentViewModel>

the Action Method on the Payment view looks like:

        [HttpGet]
    public ActionResult Payment(int id)
    {
        var payments = contractController.GetPaymentsForActiveContract(id);

        return View(payments);
    }

Now GetPaymentsForActiveContract returns a List and this works fine when I use

@Html.Partial("partialviews/_Payments")

on my view. but what i'm trying to do now is have another action method get called, which will return either a view which will display a table of payments, or another one which will write out there are none if no payments exists.

I have that action defined as:

        public ActionResult GetPayments(List<PaymentViewModel> payments)
    {
        if (payments.Count > 0)
        {
            return PartialView("partialviews/_Payments", payments);
        }
        return PartialView("partialviews/_NoPayments");
    }

I'm not sure how to pass the Model into this Action from my view. I'm looking through the @Html methods, but can't seem to find anything which would allow me to call the GetPayments method passing in the model and having it return either Partial View.

¿Fue útil?

Solución

If you want to call controller action from view, you can use

@Html.Action() 

method and pass payments list as route value

@{
    var paymentsList = new List<PaymentViewModel> {new PaymentViewModel(), new PaymentViewModel()};
}

@Html.Action("GetPayments", new { payments = paymentsList })
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top