Я новичок в Visual Studio MVC 5. Я не уверен, какой способ передачи значения из одной формы в другую форму.
Тема:
Создание договора для ведения учета клиентадетали.
У меня есть 3 формы, связанные с договором.
[Контракт] - 10 Поле {Custname, CustCode, Дата начала, Дата истечения, Плата за обслуживание, Дата выставления счета, ProdName, payFrequency, InstallmentAmount, Напоминание об истечении срока действия}
[подписка на продукт]
- [запись клиента] = 3 поля {custname, CustCode, CustContact}
Вопрос: 1. Внутри Договора, как выбрать Клиента, которого я создаю в Записи Клиента.
Сценарий для моего контракта:
Типовой контракт:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CMApp.Models
{
public class Contract
{
[Key]
public int AgrID { get; set; } // Agreement ID
[Required]
public string CustTitle { get; set; } // Customer Title, like Apple OS project
[DisplayName("Attachment File")] // Name Attachment File
public byte[] AttachmentFile { get; set; } // Declare AttachmentFile
public string AttachmentMimeType { get; set; }
[Required(ErrorMessage = "Please enter a customer ID")] // error message Cust ID,
[DisplayName("Customer ID")] // Name
public string CustID { get;set; } // Declare Customer ID
[Required(ErrorMessage = "Please Enter a Customer Name")] // Error message Cust Name
[DisplayName("Customer Name")] // Name is Customer Name
public string CustName { get; set; } // Declare CustName
[Required(ErrorMessage = "Please Enter a Contact Person")] // Error message Contact person,
[DisplayName("Contact Person")] // Name Contract Person
public string ContPerson { get; set; } // Declare ContPerson
[DisplayName("Phone")] // Name phone no
public string ContPhone { get; set; } // Declare ContPhone
[Required(ErrorMessage = "Please enter an Email Address")] // Error message Customer Email
[RegularExpression(".+\\@.+\\..+")] // Requirement Customer Email
[DisplayName("Email Address")] // Name as Email address
public string EmailAddr { get; set; } // declare Email Adddress
[DataType(DataType.MultilineText)] // set multilinetext , textarea
[DisplayName("Remark")] // Name Remark
public string Remark { get; set; } // Declare Remark
[Required] // Must key in
[DisplayName("Maintenance Fee")] // Name as Maintenance Fee
public double Fee { get; set; } // Maintenance Fee
[DisplayName("Bill No")] // Name as Maintenance Fee
public string BillNo { get; set; } // Bill No
[DisplayName("Designated Technical Name")] // Name Designated Technical Name
public string TechSupName { get; set; } // Declare technical Name
[DataType(DataType.DateTime)] // set DateTime format
[DisplayName("Bill Date")] // Name Bill Date
[DisplayFormat(DataFormatString = "{0:dd/MM/yy}")] // must display in dd/MM/yy
public Nullable<DateTime> BillDate { get; set; } // declare bill date
[DataType(DataType.DateTime)] // set DateTime format
[DisplayName("Start Date")] // Name Start Date
[DisplayFormat(DataFormatString = "{0:dd/MM/yy}")] // must display in dd/MM/yy
public DateTime StartDate { get; set; } // declare StartDate
[DataType(DataType.DateTime)] // set DateTime format
[DisplayName("End Date")] // Name End Date
[DisplayFormat(DataFormatString = "{0:dd/MM/yy}")] // must display in dd/MM/yy
public DateTime EndDate { get; set; } // declare EndDate
[DataType(DataType.DateTime)] // set DateTime format
[DisplayName("Created Date")] // Name Created Date
[DisplayFormat(DataFormatString = "{0:dd/MM/yy}")] // must display in dd/MM/yy
public DateTime CreatedDate { get; set; } // declare CreatedDate
public string UserName { get; set; }
[DisplayName("Payment Frequency")]
public string PayFreq { get; set; }
[DisplayName("Instalment Amount")]
public double IntAmt { get; set; } // instalment Amount
[DisplayName("Expiry Reminder (no of day)")]
public string ExpRem { get; set; } // Expiry Reminder
[DisplayName("Subscription Type")]
public string SubType { get; set; }
[DisplayName("Product Name")]
public string ProdName { get; set; }
public virtual ICollection<Customer> Customers { get; set; }
public virtual ICollection<Comment> Comments { get; set; } // Interface comment
public virtual ICollection<Technician> Technicians { get; set; } // Interface Technician
public virtual ICollection<Subscription> Subscriptions { get; set; } // Interface Subscription
}
}
model-ContractSharingContext
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
// 3 third to do
namespace CMApp.Models
{
public class ContractSharingContext : DbContext, IContractSharingContext
{
public ContractSharingContext():base()
{
this.Database.CommandTimeout = 180;
}
public DbSet<Customer> Customers { get; set; }
public DbSet<Contract> Contracts { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<Subscription> Subscriptions { get; set; }
public DbSet<Technician> Technicians { get; set; }
IQueryable<Customer> IContractSharingContext.Customers
{
get { return Customers; }
}
IQueryable<Contract> IContractSharingContext.Contracts
{
get { return Contracts; }
}
IQueryable<Comment> IContractSharingContext.Comments
{
get { return Comments; }
}
IQueryable<Subscription> IContractSharingContext.Subscriptions
{
get { return Subscriptions; }
}
IQueryable<Technician> IContractSharingContext.Technicians
{
get { return Technicians; }
}
int IContractSharingContext.SaveChanges()
{
return SaveChanges();
}
T IContractSharingContext.Add<T>(T entity)
{
return Set<T>().Add(entity);
}
Contract IContractSharingContext.FindContractbyID(int ID)
{
return Set<Contract>().Find(ID);
}
Contract IContractSharingContext.FindContractbyCustTitle(string CustTitle)
{
Contract contract = (from p in Set<Contract>()
where p.CustTitle == CustTitle
select p).FirstOrDefault();
return contract;
}
Customer IContractSharingContext.FindCustomerByID(int ID)
{
return Set<Customer>().Find(ID);
}
Comment IContractSharingContext.FindCommentByID(int ID)
{
return Set<Comment>().Find(ID);
}
Subscription IContractSharingContext.FindSubscriptionByID(int ID)
{
return Set<Subscription>().Find(ID);
}
Technician IContractSharingContext.FindTechnicianByID(int ID)
{
return Set<Technician>().Find(ID);
}
T IContractSharingContext.Delete<T>(T entity)
{
return Set<T>().Remove(entity);
}
}
}
model-iContractSharingContext
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 4 forth to do
namespace CMApp.Models
{
public interface IContractSharingContext
{
IQueryable<Customer> Customers { get; }
IQueryable<Contract> Contracts { get; }
IQueryable<Comment> Comments { get; }
IQueryable<Subscription> Subscriptions { get; }
IQueryable<Technician> Technicians { get; }
int SaveChanges();
T Add<T>(T entity) where T : class;
Contract FindContractbyID(int ID);
Contract FindContractbyCustTitle(string CustTitle);
Comment FindCommentByID(int ID);
Subscription FindSubscriptionByID(int ID);
Technician FindTechnicianByID(int ID);
Customer FindCustomerByID(int ID);
T Delete<T>(T entity) where T : class;
}
}
Контроллер
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using CMApp.Models;
// 6 to do is to create contract Controller
namespace CMApp.Controllers
{
[HandleError(View ="Error")]
[ValueReporter]
public class ContractController : Controller
{
private IContractSharingContext context;
public ContractController()
{
context = new ContractSharingContext();
}
public ContractController(IContractSharingContext Context)
{
context = Context;
}
public ActionResult Index() {
return View("Index");
}
[ChildActionOnly]
public ActionResult _ContractList(int number = 0)
{
List<Contract> contracts;
// List<Customer> customers; // list of customer
// List<Technician> technicians; // list of technician
if (number == 0)
{
contracts = context.Contracts.ToList();
// customers = context.Customers.ToList();
// technicians = context.Technicians.ToList();
}
else
{
contracts = (from p in context.Contracts orderby p.CreatedDate descending select p).Take(number).ToList();
// customers = (from q in context.Customers orderby q.CustName descending select q).Take(number).ToList();
// technicians = (from r in context.Technicians orderby r.TechSupName descending select r).Take(number).ToList();
}
return PartialView("_ContractList", contracts);
}
public ActionResult Display(int id)
{
Contract contract = context.FindContractbyID(id);
if (contract == null)
{
return HttpNotFound();
}
return View("Display", contract);
}
public ActionResult DisplayByCustTitle(string CustTitle)
{
Contract contract = context.FindContractbyCustTitle(CustTitle);
if (contract == null)
{
return HttpNotFound();
}
return View("Display", contract);
}
public ActionResult Create()
{
Contract newContract = new Contract();
newContract.CreatedDate = DateTime.Today;
return View("Create", newContract);
}
[HttpPost]
public ActionResult Create(Contract contract, HttpPostedFileBase image)
{
contract.CreatedDate = DateTime.Today;
if (!ModelState.IsValid)
{
return View("Create", contract);
}
else
{
if (image != null)
{
contract.AttachmentMimeType = image.ContentType;
contract.AttachmentFile = new byte[image.ContentLength];
image.InputStream.Read(contract.AttachmentFile, 0, image.ContentLength);
}
context.Add<Contract>(contract);
context.SaveChanges();
return RedirectToAction("Index");
}
}
public ActionResult Delete(int id)
{
Contract contract = context.FindContractbyID(id);
if (contract == null)
{
return HttpNotFound();
}
return View("Delete", contract);
}
[HttpPost]
[ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Contract contract = context.FindContractbyID(id);
context.Delete<Contract>(contract);
context.SaveChanges();
return RedirectToAction("Index");
}
public FileContentResult GetImage(int id)
{
Contract contract = context.FindContractbyID(id);
if (contract != null)
{
return File(contract.AttachmentFile, contract.AttachmentMimeType);
}
else
{
return null;
}
}
public ActionResult SlideShow()
{
throw new NotImplementedException("The SlideShow action is not yet ready");
}
}
}
Скрипт для клиента:
Модель
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CMApp.Models
{
public class Customer
{
[Key]
public int ID { get; set; } // Customer ID
[DisplayName("Customer Code")]
public string CustCode { get; set; } // Customer Code
[DisplayName("Name")] // Name, Customer Name
public string CustName { get; set; } // Declare Customer Name
[DisplayName("Contact Person")] // Contact Person, Customer Contact
public string CustCont { get; set; } // Declare Contact Person
[DisplayName("Phone")] // Phone, Customer Phone
public string CustPhone { get; set; } // Declare Phone
[DisplayName("Email Address")] // Email Address, Customer Email Address
public string CustEmail { get; set; } // Declare Email Address
}
}
Контроллер
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using CMApp.Models;
namespace CMApp.Models
{
[HandleError(View = "Error")]
public class CustomerController : Controller
{
private IContractSharingContext context;
public CustomerController()
{
context = new ContractSharingContext();
}
public CustomerController(IContractSharingContext Context)
{
context = Context;
}
public ActionResult AllCustomer(int number = 0)
{
List<Customer> customers;
if (number == 0)
{
customers = context.Customers.ToList();
}
else
{
customers = (from p in context.Customers orderby p.CustName descending select p).Take(number).ToList();
}
return View("AllCustomer", customers);
}
public ActionResult Customer() //make the link become "Customer"
{
return View();
}
public ActionResult Display(int id)
{
Customer customer = context.FindCustomerByID(id);
if (customer == null)
{
return HttpNotFound();
}
return View("Display", customer);
}
public ActionResult Create()
{
Customer newCustomer = new Customer();
return View("Create", newCustomer);
}
[HttpPost]
public ActionResult Create(Customer customer)
{
if (!ModelState.IsValid)
{
return View("Create", customer);
}
else
{
context.Add<Customer>(customer);
context.SaveChanges();
return RedirectToAction("AllCustomer");
}
}
public ActionResult Delete(int id)
{
Customer customer = context.FindCustomerByID(id);
if (customer == null)
{
return HttpNotFound();
}
return View("Delete", customer);
}
[HttpPost]
[ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Customer customer = context.FindCustomerByID(id);
context.Delete<Customer>(customer);
context.SaveChanges();
return RedirectToAction("AllCustomer");
}
}
}
Вид
@model IEnumerable<CMApp.Models.Customer>
@* link to Customer model *@
@{
ViewBag.Title = "Customer List";
}
<h2>Customer List</h2>
<div>
@Html.ActionLink("New Customer","Create")
</div>
@foreach (var item in Model)
{
<div class="customer-index-card">
<h3> @item.CustName </h3>
<div class="photo-metadata">
@* Customer Contact Number *@
<div>
<span class="display-label">
Contact Number :
</span>
<span class="display-field">
@Html.DisplayFor(model => item.CustPhone)
</span>
</div>
@* Customer Contact Person *@
<div>
<span class="display-label">
Contact Person :
</span>
<span class="display-field">
@Html.DisplayFor(model => item.CustCont)
</span>
</div>
@* Customer Email Address *@
<div>
<span class="display-label">
Email Address:
</span>
<span class="display-field">
@Html.DisplayFor(model => item.CustEmail)
</span>
</div>
</div>
@* Show Display Link *@
@Html.ActionLink("Display", "Display", new { id = item.ID })
@* Show Delete Link *@
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
</div>
}
![Contract](https://i.stack.imgur.com/X9lDa.jpg)