ASP. NET MVC2 Chapter 5 III

Source: Internet
Author: User
§ 5. 3 submitting orders

In this product development cycle, sportsstore will just send details of completed orders to the site administrator by e-mail. it need not store the order data in your database. however, that plan might change in the future, so to make this behavior easily changeable, You'll implement an abstract order submission service,Iordersubmitter.

 

§ 3.1 enhancing the domain model

Get started by implementing a model class for shipping details. Add a new class to yourSportsstore. DomainProject'sEntitiesFolder,

 namespace sportsstore. domain. entities {public class shippingdetails {[required (errormessage = "Please enter a name")] public string name {Get; set ;} [required (errormessage = "Please enter the first address line")] Public String line1 {Get; set;} Public String line2 {Get; set;} Public String line3 {Get; set;} [required (errormessage = "Please enter a city name")] Public String city {Get; set;} [required (errormessage = "Please enter a state name")] public String state {Get; set;} Public String zip {Get; set;} [required (errormessage = "Please enter a country name")] Public String country {Get; set ;}public bool giftwrap {Get; Set ;}}

Remember to add system. componentmodel. dataannotations assembly.

 

§ 5. 3.2 adding the "check out now" button

Returning toCart's indexView, add a button that navigates to an action calledCheckout

 
<P align = "center" class = "actionbuttons"> <a href = "<% = model. returnurl %> "> continue shopping </a> <% = html. actionlink ("check out now", "checkout") %> </P>

 

§ 5. 3.3 prompting the customer for shipping details

Add a new action,Checkout,Cartcontroller.

Public viewresult checkout () {return view (New shippingdetails ());}

And add a view for the action. Update the Code as follows:

<Asp: Content ID = "content2" contentplaceholderid = "maincontent" runat = "server"> <H2> checkout </H2> Please enter your details, and we'll ship your goods right away! <% Using (HTML. beginform () {%> <% = html. validationsummary () %> 
 

In this view, we useHtml. editorfor ()Helper class. This is an example of templated view helper. The idea of using it is to explicitly specify the HTML elements you want. (For example, using HTML. textboxfor ).

 

§ 5. 3.4 defining an order submitter di component

Now, if users want to send their request information, you can use action to mail the information through stmp. However, we need to create an interface to facilitate the occurrence of more situations.

 
Namespace sportsstore. domain. Services {public interface iordersubmitter {void submitorder (Cart cart, shippingdetails );}}

 

§ 3.5 completing cartcontroller

In order to complete cartcontroller, we need to establishIordersubmitterDependency. UpdateCartcontrollerConstructor

 

Namespace sportsstore. webui. controllers {public class cartcontroller: controller {private iproductsrepository productsrepository; private iordersubmitter ordersubmitter; Public cartcontroller (iproductsrepository productsrepository, iordersubmitter ordersubmitter) {This. productsrepository = productsrepository; this. ordersubmitter = ordersubmitter ;}}}

To achieveCheckout.CartcontrollerAdd a method

[Httppost] public actionresult checkout (Cart cart, shippingdetails) {If (Cart. lines. count = 0) modelstate. addmodelerror ("cart", "Sorry, your cart is empty! "); If (modelstate. isvalid) {ordersubmitter. submitorder (cart, shippingdetails); cart. clear (); Return view ("completed");} else // something was invalid return view (shippingdetails );}

 

§ 5. 3.6 adding a fake order submitter

Here, we need to add an inheritance class of cartcontroller.Fakeordersubmitter:

Namespace sportsstore. domain. Services {public class fakeordersubmitter: iordersubmitter {public void submitorder (Cart cart, shippingdetails ){}}}

And configure the ModelNinjectcontrollerfactoryMedium Registration

 
Private class sportsstoreservices: ninjectmodule {public override void load () {bind <iordersubmitter> (). To <fakeordersubmitter> ();}}

 

§ 5. 3.7 displaying validation errors

You may have discovered that we cannot see why if the input is incorrect. to verify that the input is correctCheckout.AspxAdd to viewHtml. validationsummary() Verification

 
<% Using (html. beginform () {%> <%: HTML. validationsummary () %>... not changed elsewhere...

In this way, when you fail to pass the verification, the following figure will appear:

 

§ 5. 3.8 displaying a "thanks for your order" screen

To end the "checkout" process, we need to add a completed view. enter sportsstore. the/views/cart folder of the webui project. right-click to add a view. Do not check the values of partial and strong types.

<Asp: Content ID = "content1" contentplaceholderid = "titlecontent" runat = "server"> sportsstore: Order submitted </ASP: content> <asp: content ID = "content2" contentplaceholderid = "maincontent" runat = "server"> <H2> thanks </H2> thanks for placing your order. we'll ship your goods as soon as possible. </ASP: content>

In this wayProgramThe following Thank-You interface will appear when you finish the process.

 

§ 3.9 implementing emailordersubmitter

Here we will discuss how to submit an email order.

 

§ 5.4 Summary

You have probably completed the sportsstore foreground program. compared with Amazon, this is far from enough, but you already have a list of products that can be displayed by category and page, a small shopping cart, and a simple cash register program.

highly separated architecture, which means that you can easily change and maintain the architecture. in the next chapter, you can add a directory list management (including the add, delete, modify, and query functions) to complete the entire project.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.