根据输入的ID填充表单

时间:2017-05-24 04:44:51

标签: asp.net asp.net-mvc-5

我目前正在使用asp.net MVC 5,我想知道如何根据输入的客户ID填写如何填写客户字段,如姓名,地址,出生日期等等。

这是我的模特。

[DisplayName("Insurance")]
public int InsuranceID { get; set; }
[ForeignKey("InsuranceID")]
public virtual Insurances Insurances { get; set; }
[DisplayName("Customer")]
public int CustomersID { get; set; }
[ForeignKey("CustomersID")]
public virtual Customers Customers { get; set; }
[Required]
public string Surname { get; set; }
[Required]
[DisplayName("First Name")]
public string Forename { get; set; }
[DisplayName("Maiden Name")]
public string MaidenName { get; set; }
[Required]
public string Sex { get; set; }
[Required]
public string Title { get; set; }
[Required]
[DisplayName("Marital Status")]
public string MaritalStatus { get; set; }
[Required]
[DisplayName("Region Code")]
public int regionCode { get; set; }
[DisplayName("Date of Birth")]
[DataType(DataType.Date)]
public DateTime dateOfBirth { get; set; }
[Required]
[RegularExpression(@"([0-9]+)", ErrorMessage = "Must be a Number.")]
public int nationalID { get; set; }
[DisplayName("Address")]
public string PlaceOfBirth { get; set; }
[Required]

public int telephone { get; set; }
[Required]
[DisplayName("E-Mail")]
public string email { get; set; }
[Required]
public string Occupation { get; set; }
[DisplayName("Occupation Code")]
public string OccupationCode { get; set; }
[Required]
public string Employer { get; set; }

public int Height { get; set; }
public int Weight { get; set; }

[DisplayName("Agent")]
public int AgentID { get; set; }
[ForeignKey("AgentID")]
public virtual Staff Staff { get; set; }

[Required]
public int Premium { get; set; }
[DisplayName("Relationship betweeen first and Second Proposal")]
public string relationshioBetweenFirstLifeandSecondLife { get; set; }

这是我的控制者。

[Authorize]
public class LifeProposalsController : Controller
{
    private EFDbContext db = new EFDbContext();

    // GET: LifeProposals
    public ActionResult Index()
    {
        var lifeProposals = db.LifeProposals.Include(l => l.Customers).Include(l => l.Insurances).Include(l => l.Staff);
        return View(lifeProposals.ToList());
    }

    // GET: LifeProposals/Details/5
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        LifeProposal lifeProposal = db.LifeProposals.Find(id);
        if (lifeProposal == null)
        {
            return HttpNotFound();
        }
        return View(lifeProposal);
    }

    // GET: LifeProposals/Create
    public ActionResult Create()
    {
        ViewBag.CustomersID = new SelectList(db.Customers, "CustomersID", "Forename");
        ViewBag.InsuranceID = new SelectList(db.Insurance, "InsurancesID", "InsuranceName");
        ViewBag.AgentID = new SelectList(db.Staff, "AgentID", "Forename");
        return View();
    }

    // POST: LifeProposals/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "referenceNo,proposalNo,InsuranceID,CustomersID,Surname,Forename,MaidenName,Sex,Title,MaritalStatus,regionCode,dateOfBirth,nationalID,PlaceOfBirth,telephone,email,Occupation,OccupationCode,Employer,Height,Weight,AgentID,Premium,relationshioBetweenFirstLifeandSecondLife")] LifeProposal lifeProposal)
    {
        if (ModelState.IsValid)
        {
            db.LifeProposals.Add(lifeProposal);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.CustomersID = new SelectList(db.Customers, "CustomersID", "Forename", lifeProposal.CustomersID);
        ViewBag.InsuranceID = new SelectList(db.Insurance, "InsurancesID", "InsuranceName", lifeProposal.InsuranceID);
        ViewBag.AgentID = new SelectList(db.Staff, "AgentID", "Forename", lifeProposal.AgentID);
        return View(lifeProposal);
    }

    // GET: LifeProposals/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        LifeProposal lifeProposal = db.LifeProposals.Find(id);
        if (lifeProposal == null)
        {
            return HttpNotFound();
        }
        ViewBag.CustomersID = new SelectList(db.Customers, "CustomersID", "Forename", lifeProposal.CustomersID);
        ViewBag.InsuranceID = new SelectList(db.Insurance, "InsurancesID", "InsuranceName", lifeProposal.InsuranceID);
        ViewBag.AgentID = new SelectList(db.Staff, "AgentID", "Forename", lifeProposal.AgentID);
        return View(lifeProposal);
    }

    // POST: LifeProposals/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "referenceNo,proposalNo,InsuranceID,CustomersID,Surname,Forename,MaidenName,Sex,Title,MaritalStatus,regionCode,dateOfBirth,nationalID,PlaceOfBirth,telephone,email,Occupation,OccupationCode,Employer,Height,Weight,AgentID,Premium,relationshioBetweenFirstLifeandSecondLife")] LifeProposal lifeProposal)
    {
        if (ModelState.IsValid)
        {
            db.Entry(lifeProposal).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.CustomersID = new SelectList(db.Customers, "CustomersID", "Forename", lifeProposal.CustomersID);
        ViewBag.InsuranceID = new SelectList(db.Insurance, "InsurancesID", "InsuranceName", lifeProposal.InsuranceID);
        ViewBag.AgentID = new SelectList(db.Staff, "AgentID", "Forename", lifeProposal.AgentID);
        return View(lifeProposal);
    }

    // GET: LifeProposals/Delete/5
    public ActionResult Delete(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        LifeProposal lifeProposal = db.LifeProposals.Find(id);
        if (lifeProposal == null)
        {
            return HttpNotFound();
        }
        return View(lifeProposal);
    }

    // POST: LifeProposals/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        LifeProposal lifeProposal = db.LifeProposals.Find(id);
        db.LifeProposals.Remove(lifeProposal);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }

enter image description here

0 个答案:

没有答案