MVC多模型和多视图形式

时间:2016-03-15 11:26:20

标签: asp.net-mvc

我想要完成的是从一个表格中获取捐赠的基本细节,然后转到第二个表格,它将获取所有必要的卡片支付数据,然后输入所有这些数据进入另一个页面将显示所有信息并允许他们在单击确认之前进行编辑,以确保将数据保存到数据库。

这是我的捐款模式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;

namespace donation3.Models
{
    public class donation
    {
        public int ID { get; set; }

        [Required(ErrorMessage = "Name is Required")]
        [Display(Name = "Name")]
        [StringLength(100)]
        public string Name { get; set; }

        private DateTime joined = DateTime.Now;
        [Display(Name = "Donation Date")]
        public DateTime DonationDate { get { return joined; } set { joined = value; } }

        [Required]
        [Display(Name = "Amount")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
        [Range(2, int.MaxValue, ErrorMessage = "Donation must be greater than 2")]
        [RegularExpression("[0-9]{1,}", ErrorMessage = "Only whole pounds can be donated")]
        public decimal Amount { get; set; }

        [Display(Name = "Tax Bonus")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
        public decimal TaxBonus { get { return Convert.ToDecimal(Amount / 100 * 22); } set { Convert.ToDecimal(Amount / 100 * 22); } }

        [Display(Name = "Comment")]
        [StringLength(200)]
        public string Comment { get; set; }
    }
}

这是我的付款方式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;

namespace donation3.Models
{
    public class payment
    {
        public int ID { get; set; }

        [Required(ErrorMessage = "Name is Required")]
        [Display(Name = "Name on card")]
        [StringLength(100)]
        public string NameOnCard { get; set; }

        [Required(ErrorMessage = "Card number is Required")]
        [Display(Name = "Card number")]
        public int CardNumber { get; set; }

        [Required(ErrorMessage = "Date is Required")]
        [Display(Name = "Valid from")]
        [DataType(DataType.Date)]
        public DateTime ValidFrom { get; set; }

        [Required(ErrorMessage = "Date is Required")]
        [Display(Name = "Expires")]
        [DataType(DataType.Date)]
        public DateTime Expires { get; set; }

        [Required(ErrorMessage = "Issue Number is Required")]
        [Display(Name = "Issue Number")]
        public int IssueNumber { get; set; }

        [Required(ErrorMessage = "Security code is Required")]
        [Display(Name = "Security code")]
        public int SecurityCode { get; set; }

        [Required(ErrorMessage = "Address is Required")]
        [Display(Name = "Address")]
        [StringLength(200)]
        public string Address { get; set; }

        [Required(ErrorMessage = "City is Required")]
        [Display(Name = "City")]
        [StringLength(100)]
        public string City { get; set; }

        [Required(ErrorMessage = "State is Required")]
        [Display(Name = "State")]
        public string State { get; set; }

        [Required(ErrorMessage = "Country is Required")]
        [Display(Name = "Country")]
        [StringLength(100)]
        public string Country { get; set; }

        [Required(ErrorMessage = "Postcode is Required")]
        [Display(Name = "Postcode")]
        //[RegularExpression(@"(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKPSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})")]
        public string Postcode { get; set; }

        [Required(ErrorMessage = "Email is Required")]
        [Display(Name = "Email")]
        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                            @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                            @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",
                            ErrorMessage = "Email is not valid")]
        public string Email { get; set; }
    }
}

这是我的捐赠控制员

using System.Linq;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Data.Entity;
using donation3.Models;
using System;
using System.IO;

namespace donation3.Controllers
{
    public class donationsController : Controller
    {
        private ApplicationDbContext _context;

        public donationsController(ApplicationDbContext context)
        {
            _context = context;    
        }

        // GET: donations
        public IActionResult Index()
        {
            return View(_context.donation.ToList());
        }

        //go to next form
        public IActionResult paymentDetails()
        {
            return View("/Views/payments/Create.cshtml");
        }


        // GET: donations/Details/5
        public IActionResult Details(int? id)
        {
            if (id == null)
            {
                return HttpNotFound();
            }

            donation donation = _context.donation.Single(m => m.ID == id);
            if (donation == null)
            {
                return HttpNotFound();
            }

            return View(donation);
        }

        // GET: donations/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: donations/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(donation donation)
        {
            if (ModelState.IsValid)
            {

               return RedirectToAction("paymentDetails");
            }
            return View(donation);
        }

        // GET: donations/Edit/5
        public IActionResult Edit(int? id)
        {
            if (id == null)
            {
                return HttpNotFound();
            }

            donation donation = _context.donation.Single(m => m.ID == id);
            if (donation == null)
            {
                return HttpNotFound();
            }
            return View(donation);
        }

        // POST: donations/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Edit(donation donation)
        {
            if (ModelState.IsValid)
            {
                _context.Update(donation);
                _context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(donation);
        }

        // GET: donations/Delete/5
        [ActionName("Delete")]
        public IActionResult Delete(int? id)
        {
            if (id == null)
            {
                return HttpNotFound();
            }

            donation donation = _context.donation.Single(m => m.ID == id);
            if (donation == null)
            {
                return HttpNotFound();
            }

            return View(donation);
        }

        // POST: donations/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public IActionResult DeleteConfirmed(int id)
        {
            donation donation = _context.donation.Single(m => m.ID == id);
            _context.donation.Remove(donation);
            _context.SaveChanges();
            return RedirectToAction("Index");
        }






    }
}

这是我的付款控制器

using System.Linq;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Data.Entity;
using donation3.Models;

namespace donation3.Controllers
{
    public class paymentsController : Controller
    {

        private ApplicationDbContext _context;

        public paymentsController(ApplicationDbContext context)
        {
            _context = context;    
        }



        // GET: payments
        public IActionResult Index()
        {
            return View(_context.payment.ToList());
        }

        // GET: payments/Details/5
        public IActionResult Details(int? id)
        {
            if (id == null)
            {
                return HttpNotFound();
            }

            payment payment = _context.payment.Single(m => m.ID == id);
            if (payment == null)
            {
                return HttpNotFound();
            }

            return View(payment);
        }

        // GET: payments/Create
        [HttpGet]
        [ValidateAntiForgeryToken]
        public IActionResult Create()
        {
            return View();
        }

        // POST: payments/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(payment payment)
        {
            if (ModelState.IsValid)
            {
                _context.payment.Add(payment);
                _context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(payment);
        }

        // GET: payments/Edit/5
        public IActionResult Edit(int? id)
        {
            if (id == null)
            {
                return HttpNotFound();
            }

            payment payment = _context.payment.Single(m => m.ID == id);
            if (payment == null)
            {
                return HttpNotFound();
            }
            return View(payment);
        }

        // POST: payments/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Edit(payment payment)
        {
            if (ModelState.IsValid)
            {
                _context.Update(payment);
                _context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(payment);
        }

        // GET: payments/Delete/5
        [ActionName("Delete")]
        public IActionResult Delete(int? id)
        {
            if (id == null)
            {
                return HttpNotFound();
            }

            payment payment = _context.payment.Single(m => m.ID == id);
            if (payment == null)
            {
                return HttpNotFound();
            }

            return View(payment);
        }

        // POST: payments/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public IActionResult DeleteConfirmed(int id)
        {
            payment payment = _context.payment.Single(m => m.ID == id);
            _context.payment.Remove(payment);
            _context.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}

以下是我从一种形式到另一种形式的方式

//go to next form
        public IActionResult paymentDetails()
        {
            return View("/Views/payments/Create.cshtml");
        }

所以我的问题是

  1. 我如何从一种形式转到另一种形式
  2. 如何在用户确认创建之前保留数据
  3. 如何使用确认/编辑按钮创建显示所有输入详细信息的新页面
  4. 提前致谢:)

1 个答案:

答案 0 :(得分:0)

您好我不打算为您编写代码,因为作为开发人员,您需要自己学习如何找到它并且不是那么困难,所以我将提供一些指导。此外,我不会保证这些是最好的方法,但它们会起作用

1.您可以创建按钮标记并使用Url.Action设置页面导航的href属性。您还可以使用Html.Editor或Html.EditorFor并设置其htmlattributes以使其成为按钮。

2.您可以将数据保存在Session对象中,您可以从模型或视图模型中分配其值,因为我没有读取您的整个代码。您可以将数据保存在存储临时数据的表中,也可以使用其他选项。

3.您可以根据要在视图中显示的内容,为视图模型中的任何属性创建最终页面设置的视图模型。当您从第二页转到最后一页,即详细信息页面时,当您在第二页上提交按钮时,您将调用一种操作方法。因此,在该操作方法中,您将初始化视图模型并将其传递给详细信息页面的视图。

我希望这个答案能让你满意