提问人:spencer 提问时间:11/9/2023 更新时间:11/9/2023 访问量:8
在 MVC 应用程序中将数据库数据从一个视图传递到另一个视图
Passing database data from one view to another in a MVC appliaction
问:
希望你们中的一个人能够为我指出正确的方向,我刚刚踏上了学习 c# 和 .net 的旅程,我正在练习应用程序,它将存储联系人/客户、潜在客户和合同。我已经构建了一个带有存储、召回和编辑的联系人类的模型类,但我需要将其中一些数据合并到潜在客户部分,然后将此潜在客户存储在联系人区域中。
我在网上看了一下,它建议使用tempdata,在研究如何做到这一点之前,我希望你们能够为新手找到最佳解决方案,并对我如何实现这一点有一点了解。
namespace CRM.Models
{
public class Contacts
{
[Key]
public int ContactId { get; set; }
[DisplayName("Contact Number")]
public int? ContactNumber { get; set; }
[DisplayName("First Name")]
[Required]
public string ContactFirstName { get; set; }
[DisplayName("Last Name")]
[Required]
public string ContactLastName { get; set; }
[DisplayName("Company Name")]
public string? CompanyName { get; set; }
[DisplayName("Main Telephone")]
public string? Telephone { get; set; }
[DisplayName("Mobile")]
public string? Mobile { get; set; }
[DisplayName("Main Email")]
public string? email { get; set; }
[DisplayName("Work Email")]
public string? WorkEmail { get; set; }
[DisplayName("Address 1")]
public string? Address1 { get; set; }
[DisplayName("Address 2")]
public string? address2 { get; set; }
public string? City { get; set; }
[DisplayName("County / State")]
public string? State { get; set; }
[DisplayName("Postcode / Zip")]
public string? Postcode { get; set; }
[DisplayName("Contact Type")]
[Required(ErrorMessage = "Please select a contact type.")]
public ContactType ContactType { get; set; }
[DisplayName("Accounts Ref")]
public string? AccountsRef { get; set; }
[DisplayName("Customer Portal Access")]
public bool CustomerPortal { get; set; }
[DisplayName("Access to Docs")]
public bool AccessDocs { get; set; }
[DisplayName("Email")]
public string? PortalEmail { get; set; }
[DisplayName("Password")]
public string? PortalPassword { get; set; }
[DisplayName("Comments")]
public string? ContactComments { get; set; }
}
}
我假设我需要在此处添加将存储潜在客户、合同的项目,但我主要关心的是如何将姓名、地址和联系方式传递给潜在客户表单/视图。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using CRM.Models;
namespace CRM.Controllers
{
public class ContactsController : Controller
{
private readonly ContactDbContext _context;
public ContactsController(ContactDbContext context)
{
_context = context;
}
// GET: Contacts
public async Task<IActionResult> Index()
{
return _context.Contacts != null ?
View(await _context.Contacts.ToListAsync()) :
Problem("Entity set 'ContactDbContext.Contacts' is null.");
}
// GET: Contacts/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null || _context.Contacts == null)
{
return NotFound();
}
var contacts = await _context.Contacts
.FirstOrDefaultAsync(m => m.ContactId == id);
if (contacts == null)
{
return NotFound();
}
return View(contacts);
}
// GET: Contacts/Create
public IActionResult Create()
{
return View();
}
// POST: Contacts/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ContactId,ContactNumber,ContactFirstName,ContactLastName,CompanyName,Telephone,Mobile,email,WorkEmail,Address1,address2,City,State,Postcode,ContactType,AccountsRef,CustomerPortal,AccessDocs,PortalEmail,PortalPassword,ContactComments")] Contacts contacts)
{
if (ModelState.IsValid)
{
_context.Add(contacts);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(contacts);
}
// GET: Contacts/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null || _context.Contacts == null)
{
return NotFound();
}
var contacts = await _context.Contacts.FindAsync(id);
if (contacts == null)
{
return NotFound();
}
return View(contacts);
}
// POST: Contacts/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("ContactId,ContactNumber,ContactFirstName,ContactLastName,CompanyName,Telephone,Mobile,email,WorkEmail,Address1,address2,City,State,Postcode,ContactType,AccountsRef,CustomerPortal,AccessDocs,PortalEmail,PortalPassword,ContactComments")] Contacts contacts)
{
if (id != contacts.ContactId)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(contacts);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ContactsExists(contacts.ContactId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(contacts);
}
// GET: Contacts/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null || _context.Contacts == null)
{
return NotFound();
}
var contacts = await _context.Contacts
.FirstOrDefaultAsync(m => m.ContactId == id);
if (contacts == null)
{
return NotFound();
}
return View(contacts);
}
// POST: Contacts/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
if (_context.Contacts == null)
{
return Problem("Entity set 'ContactDbContext.Contacts' is null.");
}
var contacts = await _context.Contacts.FindAsync(id);
if (contacts != null)
{
_context.Contacts.Remove(contacts);
}
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool ContactsExists(int id)
{
return (_context.Contacts?.Any(e => e.ContactId == id)).GetValueOrDefault();
}
}
}
任何帮助或建议都将不胜感激。
我的想法是,我可能会将所有数据存储在一个类中,但我认为这将是一个骗局,尽管它可能有效,但我无法正确处理表单中的验证,理想情况下,laed 部分中的搜索区域将是将所需详细信息填充到新的潜在客户表单中的最佳选择。
答: 暂无答案
评论