Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Gary Prothero 14 posts 34 karma points
    Mar 27, 2013 @ 17:26
    Gary Prothero
    0

    MVC - Passing data from template to surfacecontroller

    I cannot seem to figure out how to pass parameters from my template into to the View so I can preset my dropdown list selected item prior to displaying the form.

    I have tried to pass data in the Html.Action call, but it doesn't come across as ViewData in either the surface controller or View code.

    How do I access name and age in my surfacecontroller. I have tried ViewData["age"].Tostring()

    In Template:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
    Layout = "SubscriptionPages.cshtml";
    }

    @section scripts{
    <script src="/scripts/subscription.js"></script>
    }

    @Html.Action("SubscriptionPaymentView","PaymentSurface", new { name = "Ned", age = 28 })

     

    In SurfaceController:

    using System.Web;
    using System.Web.Security;
    using System.Web.Mvc;
    using umbraco;
    using Umbraco.Core;
    using umbraco.cms.businesslogic.member;
    using Umbraco.Web.Mvc;
    using Directory.Providers;
    using Umbraco.Web.Dynamics;

    namespace DirectoryV6.Controllers.SurfaceControllers
    {
    public class PaymentSurfaceController : SurfaceController
    {
    [HttpGet]
    [ActionName("SubscriptionPaymentView")]
    public ActionResult SubscriptionPaymentGet()
    {
    return PartialView("AuthorizeNetPaymentView", new Models.PaymentViewModel());
    }

    [HttpPost]
    [ActionName("SubscriptionPaymentView")]
    public ActionResult HandleSubscriptionPaymentPost(Models.PaymentViewModel model, int? completeNodeId )
    {
    if (!ModelState.IsValid)
    {
    return CurrentUmbracoPage();
    }

    Providers.Common.OrderData orderData = new Providers.Common.OrderData();

    decimal amount = 0;
    if (decimal.TryParse(model.Amount, out amount))
    {
    orderData.OrderTotal = amount;
    }
    orderData.CreditCardNumber = model.CreditCardNumber;
    orderData.CreditCardSecurityCode = model.CreditCardSecurityCode;
    orderData.CreditCardExpireMonth = model.CreditCardExpireMonth;
    orderData.CreditCardExpireYear = model.CreditCardExpireYear;
    orderData.FirstName = model.FirstName;
    orderData.LastName = model.LastName;
    orderData.Email = model.Email;
    orderData.Phone = model.Phone;
    orderData.BillingPostalCode = model.BillingPostalCode;
    orderData.BillingAddress = model.BillingAddress;
    orderData.BillingAddress2 = model.BillingAddress2;
    orderData.BillingCity = model.BillingCity;
    orderData.BillingStateOrRegion = model.BillingStateOrRegion;
    orderData.BillingCountry = model.BillingCountry;
    orderData.Description = "Bronze";

    if (AuthorizeNetPaymentProvider.PWCreateSubscription(orderData, AuthorizeNetPaymentProvider.SubscriptionType.Annual) == false)
    {
    TempData["order"] = orderData;
    return RedirectToCurrentUmbracoPage();
    }

    var cs = ApplicationContext.Current.Services.ContentService;

    Member member = Member.GetCurrentMember();

    string[] s = member.getProperty("subscription").Value.ToString().Split(',');

    Umbraco.Core.Models.IContent type = cs.GetChildren(1155).Where(x => x.Name == model.SubscriptionType).SingleOrDefault();

    if( type != null)
    {
    string newId = type.Id.ToString();

    if (s.Count() > 0)
    {
    member.getProperty("subscription").Value = "," + newId;
    member.getProperty("subscriptionIds").Value = string.Format(",{0}:{1}",newId, orderData.SubscriptionId);
    }
    else
    {
    member.getProperty("subscription").Value = newId;
    member.getProperty("subscriptionIds").Value = string.Format("{0}:{1}",newId, orderData.SubscriptionId);
    }
    }

    return RedirectToUmbracoPage(completeNodeId.Value);
    }

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Apr 25, 2013 @ 04:23
    Shannon Deminick
    0

    Hi @Gary, 

    You are trying to render a Child Action with:

    @Html.Action("SubscriptionPaymentView","PaymentSurface", new { name = "Ned", age = 28 })

    But you have no action that is marked to be a ChildAction, normally you need to mark an action with [ChildActionOnly] (here's more on child actions: http://our.umbraco.org/documentation/Reference/Mvc/child-actions)

    You have 2 actions that match the name "SubscriptionPaymentView" and neither of them match the route parameters that you are trying to pass in to them. The first one doesn't accept any parameters: public ActionResult SubscriptionPaymentGet() and the second one public ActionResult HandleSubscriptionPaymentPost(Models.PaymentViewModel model, int? completeNodeId ) accepts completely differnet types of parameters.

    If you want to render a child action with parameters please follow the tutorial on the link above. For your example you would do something like:

    @Html.Action("MyChildAction","PaymentSurface", new { name = "Ned", age = 28 })

    Then define your child action like:

    [ChildActionOnly]
    public ActionResult MyChildAction(string name, int age)
    {...}

     

Please Sign in or register to post replies

Write your reply to:

Draft