ABP additions and deletions to check code snippets __MVC

Source: Internet
Author: User
@using System.Web.Optimization @using MultiPageSimpleTask.Entitys.Dtos;
    @model ilist<productdto> @{Layout = null;
Productdto productdto = viewbag.singleproduct as productdto; } <!doctype html>  

Using System;
Using System.Text;
Using System.Collections.Generic;
Using SYSTEM.WEB.MVC;
Using Abp.automapper;
Using Abp.Domain.Uow;
Using AutoMapper;
Using Multipagesimpletask.entitys;


Using MultiPageSimpleTask.Entitys.Dtos;
        Namespace MultiPageSimpleTask.Web.Controllers {public class Homecontroller:multipagesimpletaskcontrollerbase {
        Private readonly statusmsg _result = new Statusmsg ();

        Private ReadOnly icategoryrepository _categoryappservice;
        Private ReadOnly iproductrepository _productappservice;

        Private ReadOnly Iunitofworkmanager _unitofworkmanager; Public HomeController (Icategoryrepository categoryappservice, Iproductrepository Productapps
            Ervice, Iunitofworkmanager unitofworkmanager) {_categoryappservice = Categoryappservice;
            _productappservice = Productappservice;
        _unitofworkmanager = Unitofworkmanager; #region Home Public ActIonresult Index () {String strSQL = "Select ProductID as id,* from product";//must add alias ID column, otherwise the error
            String orderfied = "ORDER by ProductId";
            int totalcount = 0;
            int pagecount = 0; Get a page list of items var lists = _productappservice.getquerypager (strSQL, 2, orderfied, ref totalcount, ref Pagecoun
            T);
            Todo:used AutoMapper to automatically convert list<product> to List<productdto>.
            list<productdto> produscts = mapper.map<list<productdto>> (lists);
            Get a product var product = _productappservice.get (2);
            Viewbag.singleproduct = mapper.map<productdto> (product);
            Viewbag.totalcount = TotalCount;
            Viewbag.pagecount = PageCount;
        Return View (produscts); #endregion #region New Products [HttpGet] public ActionResult Add () {Entit Ys. Product Product = new Entitys.product ();
        return View (product); [HttpPost] public actionresult ADD (entitys.product model) {_result.
            Status = false; Todo:convert to DTOs if (! Modelstate.isvalid) {_result.
            Message = Expenderrors (this);  else {int i = _productappservice.insertandgetid (model);//Add If
                (i > 0) {_result.
                    Status = true; _result.
                message = i.ToString ();
        return new Jsonresult () {Data = _result};
            #endregion #region Modify the product [HttpGet] public actionresult Update (int id) {
            var model = _productappservice.get (ID);
        return View (model); [HttpPost] public actionresult Update (entitys.product model) {_result. StaTus = false; Todo:convert to DTOs if (! Modelstate.isvalid) {_result.
            Message = Expenderrors (this); else {var modeltarget = _productappservice.get (model. ID); Assign value Modeltarget.productname = model. ProductName; Update field modeltarget.discontinued = model.
                Discontinued; int i = _productappservice.insertorupdateandgetid (modeltarget); Add if (i > 0) {_result.
                    Status = true; _result.
                message = i.ToString ();
        return new Jsonresult () {Data = _result}; #endregion #region Bulk New products///<summary>///bulk New products (enable work units)///</summ ary>///<returns></returns> public ActionResult Addproductbatch () {Li St<productinput> Inputs= new List<productinput> (); Inputs.
                Add (new Productinput {ProductName = "new Product 222", SupplierID = 1,
            CategoryID = 2, discontinued = TRUE//Set data does not conform to all uncommitted}); Inputs.
                Add (new Productinput {ProductName = "new Product 456", SupplierID = 1,

            CategoryID = 2, discontinued = false}); _result.
            Status = false;

            StringBuilder builder = new StringBuilder ();
                    using (var unitofwork = _unitofworkmanager.begin ())//enable work unit {try {
                        foreach (var input in inputs) {//convert to DTOs var model = input.
                        Mapto<entitys.product> (); _productappservice.insertandgetid (model); Add} UNItofwork.complete (); Commit TRANSACTION} catch (Exception ex) {builder. Appendline (ex.
                message); }} _result. Status = Builder. Length = = 0?
            True:false; _result. Message = Builder. Length = = 0? "": Builder.
            ToString ();
        return new Jsonresult () {Data = _result};
        #endregion #region Delete Merchandise///<summary>///Delete merchandise///</summary> <param name= "id" > Product id</param>///<returns></returns> [HttpPost] Pu Blic actionresult Delete (int id) {_result.
            Status = false; Todo:convert to DTOs if (! Modelstate.isvalid) {_result.
            Message = Expenderrors (this); else {try {_productappservice.delete (ID);
         Delete           _result.
                Status = true; The catch (Exception ex) {_result.
                Status = false;
        return new Jsonresult () {Data = _result};
        #endregion #region return message class///<summary>///return information class///</summary> public class Statusmsg {///<summary>///return status true False///<
            /summary> public bool Status; <summary>///return information such as 400, 401 status code, or other information///</summary> public string Mess
            Age
        <summary>///returns extended information///</summary> public object result;
            #endregion #region Get validation information public static string expenderrors (Controller Controller) { System.Text.StringBuilder sberrors = new System.Text.StringBuiLder (); foreach (var item in controller.) Modelstate.values) {if (item). Errors.Count > 0) {for (int i = Item. errors.count-1; I >= 0; i--) {sberrors.appendline (item). Errors[i].
                    ErrorMessage);
        }} return sberrors.tostring (); } #endregion}}

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.