Umbraco parsing value from route url to model on the view

258 views Asked by At

I am able to return static data on my view though my viewmodel, but how do I parse value from the url to my model at the view?

Like https://localhost:44354/Products/1

Here is my code:

ProductPageController.cs

public  ActionResult Index(ContentModel model, int id)
        {
            var productViewModel = new MyProductViewModel(model.Content);
            productViewModel.productId = id;

            return CurrentTemplate(productViewModel);
        }

RegisterCustomRouteComponen.cs, route

 public class RegisterCustomRouteComponent : IComponent
    {
        public void Initialize()
        {
            #region Custom Routes
            // You can now regiter your custom routes as normal
            RouteTable.Routes.MapRoute(
                "Products page",
                "products/{action}/{id}",
                new
                {
                    controller = "ProductPage",
                    action = "Index",
                    id = UrlParameter.Optional
                });
            #endregion
        }

        public void Terminate()
        {
        }
    }

MyProductViewModel.cs

    {
        // The PublishedContentWrapped accepts an IPublishedContent item as a constructor
        public MyProductViewModel(IPublishedContent content) : base(content) { }

        // Custom properties here...
        public int StockLevel = 1;

        public int productId { get; set; }



    }
1

There are 1 answers

0
Vladimir On

You can get values from your url with Request.QueryString["yourvalue"]; Store it as a variable and then assign it to whatever you want in your model.