The desired effect is to achieve 90 percent or 80 percent of the total price of all items in the cart:
When you click on "90 percent":
When you click on "80 percent":
-Ideas
80 percent or 90 percent is the different implementation of the discounted interface, the key is: what conditions to decide which discount method to use?
--When you click on the 80 percent or 90 percent link, put the parameters in the route, and then in the custom controller factory, depending on the parameters of the different options to use which discount method.
-model
public class Cartline
{
public int Id {get; set;}
public string Name {get; set;}
Public decimal price {get; set;}
public int Quantity {get; set;}
}
-Interface
Using Mvcapplication2.models;
Namespace MvcApplication2
{
public interface Idiscount
{
Decimal Getfinalprice (list<cartline> cartlines);
}
}
-2 implementations of the interface
using System.Collections.Generic;
namespace Mvcapplication2.implementation
{
Public class Ninediscount:idiscount
{
Public decimal Getfinalprice (list<models.cartline> cartlines)
{
decimal result = 0.0M;
foreach in Cartlines)
{
Result + = Item. Price*item. Quantity;
}
return result* (90m/100m);
}
}
}
using System.Collections.Generic;
namespace Mvcapplication2.implementation
{
Public class Eightdiscount:idiscount
{
Public decimal Getfinalprice (list<models.cartline> cartlines)
{
decimal result = 0.0M;
foreach in Cartlines)
{
Result + = Item. Price * Item. Quantity;
}
return result * (80m/100m);
}
}
}
-homecontroller
using System.Collections.Generic;
using SYSTEM.WEB.MVC;
using Mvcapplication2.models;
namespace Mvcapplication2.controllers
{
Public class Homecontroller:controller
{
Public ActionResult Index ()
{
New List<cartline> ()
{
New "Product1", Price = 80M, Quantity = 2},
New "Product2", Price = 100M, Quantity = 3},
};
session["cart"] = cartlines;
return View (Cartlines);
}
}
}
-home/index.cshtml
The different discount methods are placed in the route to pass.
@model list<mvcapplication2.models.cartline>
@{
"Index";
"~/views/shared/_layout.cshtml";
}
<table>
<tr style="Background-color: #e3e3e3;" >
<td> Products </td>
<td> Price </td>
<td> Quantity </td>
</tr>
@foreach in Model)
{
<tr>
<td> @item. Name</td>
<td>@string. Format ("{0:c}", item. Price) </td>
<td> @item. Quantity</td>
</tr>
}
</table>
<p>
@Html. ActionLink ("90 percent purchase" "Index""shop"new"Nine"},new {})
</p>
<p>
@Html. ActionLink ("80 percent purchase""Index""shop"new"Eight"},new {})
</p>
-Custom controller factory, using Ninject, depending on the routing parameters policy, decided to choose a specific discount interface implementation
using System;
using SYSTEM.WEB.MVC;
using System.Web.Routing;
using Mvcapplication2.implementation;
using Ninject;
namespace Mvcapplication2.extension
{
Public class Ninjectcontrollerfactory:defaultcontrollerfactory
{
Ikernel Ninjectkernel;
string "";
Public Ninjectcontrollerfactory ()
{
New Standardkernel ();
}
protected Override IController getcontrollerinstance (RequestContext requestcontext, Type controllertype)
{
if (requestcontext.routedata.values["policy"null)
{
Policy = requestcontext.routedata.values["policy"]. ToString ();
}
Addbindings ();
return NULL null : (IController) Ninjectkernel.get (Controllertype);
}
Private void Addbindings ()
{
Switch (policy)
{
Case "Eight":
Ninjectkernel.rebind<idiscount> (). To<eightdiscount> ();
break;
Case "Nine":
Ninjectkernel.rebind<idiscount> (). To<ninediscount> ();
break;
default:
Ninjectkernel.rebind<idiscount> (). To<ninediscount> ();
break;
}
}
}
}
-Custom controller Factory Global Registration
ControllerBuilder.Current.SetControllerFactory (New Ninjectcontrollerfactory ());
Using the discounted interface method in-shopcontroller
using System;
using System.Collections.Generic;
using SYSTEM.WEB.MVC;
using Mvcapplication2.models;
namespace Mvcapplication2.controllers
{
Public class Shopcontroller:controller
{
Public Idiscount _discount;
Public Shopcontroller (Idiscount discount)
{
this. _discount = Discount;
}
Public ActionResult Index (string policy)
{
New List<cartline> ();
if (session["cart"null)
{
Cartlines = (list<cartline>) session["cart"];
}
viewdata["Total"= String.Format ("{0:c}", _discount.getfinalprice (Cartlines));
return View ();
}
}
}
-shop/index.cshtml
@{
"Index";
"~/views/shared/_layout.cshtml";
}
The price after the discount is: @ViewData ["Total"]
-Custom Routing
In order to make the URL more intuitive, conform to Controller/action/paramter:
Routes. MapRoute (
"Default",
"{controller}/{action}/{policy}",
New "Home" "Index", policy = urlparameter.optional}
);