C# ASP.NET MVC使用DropDownList编辑视图模型
在编辑操作中检索DropDownList控件的正确值时遇到问题 客户型号:C# ASP.NET MVC使用DropDownList编辑视图模型,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,在编辑操作中检索DropDownList控件的正确值时遇到问题 客户型号: public class Customer { public int CustId { get; set; } public string CustDisplayName { get; set; } public string CustFirstName { get; set; } public string CustLastName { get; set; } publi
public class Customer
{
public int CustId { get; set; }
public string CustDisplayName { get; set; }
public string CustFirstName { get; set; }
public string CustLastName { get; set; }
public string CustCompanyName { get; set; }
public string CustAddress { get; set; }
public string CustPhoneNumber { get; set; }
public string CustMobileNumber { get; set; }
public string CustEmailAddress { get; set; }
public int StId { get; set; }
public State State { get; set; }
}
public class State
{
public int StId { get; set; }
public string StAbbr { get; set; }
public List<Customer> Customers { get; set; }
}
public class Manufacturer
{
public int MfrId { get; set; }
public string MfrCompanyName { get; set; }
public string MfrWebsiteDomainName { get; set; }
}
public class CustomerController : Controller
{
private WebAppDbContext _context;
public CustomerController(WebAppDbContext context)
{
_context = context;
}
// GET: /<Customer>/
public IActionResult Index()
{
return View(_context.Customers.ToList());
}
public ActionResult Create()
{
var states = _context.States.ToList();
var viewModel = new CustomerFormViewModel
{
States = states
};
return View(viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CustomerFormViewModel vm)
{
if (ModelState.IsValid)
{
var customer = new Customer();
{
customer.CustDisplayName = vm.CustDisplayName;
customer.CustFirstName = vm.CustFirstName;
customer.CustLastName = vm.CustLastName;
customer.CustCompanyName = vm.CustCompanyName;
customer.CustAddress = vm.CustAddress;
customer.CustPhoneNumber = vm.CustPhoneNumber;
customer.CustMobileNumber = vm.CustMobileNumber;
customer.CustEmailAddress = vm.CustEmailAddress;
customer.StId = vm.StId;
}
_context.Customers.Add(customer);
_context.SaveChanges();
return RedirectToAction("Index");
}
else
{
vm.States = _context.States.ToList();
return View(vm);
}
}
public ActionResult Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var customervm = new CustomerFormViewModel();
{
Customer customer = _context.Customers.SingleOrDefault(c => c.CustId == id);
if (customer == null)
{
return NotFound();
}
customervm.CustId = customer.CustId;
customervm.CustDisplayName = customer.CustDisplayName;
customervm.CustFirstName = customer.CustFirstName;
customervm.CustLastName = customer.CustLastName;
customervm.CustCompanyName = customer.CustCompanyName;
customervm.CustAddress = customer.CustAddress;
customervm.CustPhoneNumber = customer.CustPhoneNumber;
customervm.CustMobileNumber = customer.CustMobileNumber;
customervm.CustEmailAddress = customer.CustEmailAddress;
customervm.StId = customer.StId;
}
return View(customervm);
}
}
<div class="form-group">
@Html.LabelFor(c => c.CustDisplayName)
@Html.TextBoxFor(c => c.CustDisplayName, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustDisplayName)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustFirstName)
@Html.TextBoxFor(c => c.CustFirstName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustLastName)
@Html.TextBoxFor(c => c.CustLastName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustCompanyName)
@Html.TextBoxFor(c => c.CustCompanyName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustAddress)
@Html.TextBoxFor(c => c.CustAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustAddress)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustPhoneNumber)
@Html.TextBoxFor(c => c.CustPhoneNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustPhoneNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustMobileNumber)
@Html.TextBoxFor(c => c.CustMobileNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustMobileNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustEmailAddress)
@Html.TextBoxFor(c => c.CustEmailAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustEmailAddress)
</div>
<div class="form-group">
@Html.LabelFor(s => s.StId)
@Html.DropDownListFor(s => s.StId, new SelectList(Model.States, "StId", "StAbbr"), "", new { @class = "form-control" })
@Html.ValidationMessageFor(s => s.StId)
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustDisplayName)
@Html.TextBoxFor(c => c.CustDisplayName, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustDisplayName)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustFirstName)
@Html.TextBoxFor(c => c.CustFirstName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustLastName)
@Html.TextBoxFor(c => c.CustLastName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustCompanyName)
@Html.TextBoxFor(c => c.CustCompanyName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustAddress)
@Html.TextBoxFor(c => c.CustAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustAddress)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustPhoneNumber)
@Html.TextBoxFor(c => c.CustPhoneNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustPhoneNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustMobileNumber)
@Html.TextBoxFor(c => c.CustMobileNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustMobileNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustEmailAddress)
@Html.TextBoxFor(c => c.CustEmailAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustEmailAddress)
</div>
<div class="form-group">
@Html.LabelFor(s => s.StId)
@Html.DropDownListFor(s => s.StId, new SelectList(Model.States, "StId", "StAbbr"), "", new { @class = "form-control" })
@Html.ValidationMessageFor(s => s.StId)
</div>
@Html.HiddenFor(c => c.CustId)
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
状态模型:
public class Customer
{
public int CustId { get; set; }
public string CustDisplayName { get; set; }
public string CustFirstName { get; set; }
public string CustLastName { get; set; }
public string CustCompanyName { get; set; }
public string CustAddress { get; set; }
public string CustPhoneNumber { get; set; }
public string CustMobileNumber { get; set; }
public string CustEmailAddress { get; set; }
public int StId { get; set; }
public State State { get; set; }
}
public class State
{
public int StId { get; set; }
public string StAbbr { get; set; }
public List<Customer> Customers { get; set; }
}
public class Manufacturer
{
public int MfrId { get; set; }
public string MfrCompanyName { get; set; }
public string MfrWebsiteDomainName { get; set; }
}
public class CustomerController : Controller
{
private WebAppDbContext _context;
public CustomerController(WebAppDbContext context)
{
_context = context;
}
// GET: /<Customer>/
public IActionResult Index()
{
return View(_context.Customers.ToList());
}
public ActionResult Create()
{
var states = _context.States.ToList();
var viewModel = new CustomerFormViewModel
{
States = states
};
return View(viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CustomerFormViewModel vm)
{
if (ModelState.IsValid)
{
var customer = new Customer();
{
customer.CustDisplayName = vm.CustDisplayName;
customer.CustFirstName = vm.CustFirstName;
customer.CustLastName = vm.CustLastName;
customer.CustCompanyName = vm.CustCompanyName;
customer.CustAddress = vm.CustAddress;
customer.CustPhoneNumber = vm.CustPhoneNumber;
customer.CustMobileNumber = vm.CustMobileNumber;
customer.CustEmailAddress = vm.CustEmailAddress;
customer.StId = vm.StId;
}
_context.Customers.Add(customer);
_context.SaveChanges();
return RedirectToAction("Index");
}
else
{
vm.States = _context.States.ToList();
return View(vm);
}
}
public ActionResult Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var customervm = new CustomerFormViewModel();
{
Customer customer = _context.Customers.SingleOrDefault(c => c.CustId == id);
if (customer == null)
{
return NotFound();
}
customervm.CustId = customer.CustId;
customervm.CustDisplayName = customer.CustDisplayName;
customervm.CustFirstName = customer.CustFirstName;
customervm.CustLastName = customer.CustLastName;
customervm.CustCompanyName = customer.CustCompanyName;
customervm.CustAddress = customer.CustAddress;
customervm.CustPhoneNumber = customer.CustPhoneNumber;
customervm.CustMobileNumber = customer.CustMobileNumber;
customervm.CustEmailAddress = customer.CustEmailAddress;
customervm.StId = customer.StId;
}
return View(customervm);
}
}
<div class="form-group">
@Html.LabelFor(c => c.CustDisplayName)
@Html.TextBoxFor(c => c.CustDisplayName, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustDisplayName)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustFirstName)
@Html.TextBoxFor(c => c.CustFirstName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustLastName)
@Html.TextBoxFor(c => c.CustLastName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustCompanyName)
@Html.TextBoxFor(c => c.CustCompanyName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustAddress)
@Html.TextBoxFor(c => c.CustAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustAddress)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustPhoneNumber)
@Html.TextBoxFor(c => c.CustPhoneNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustPhoneNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustMobileNumber)
@Html.TextBoxFor(c => c.CustMobileNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustMobileNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustEmailAddress)
@Html.TextBoxFor(c => c.CustEmailAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustEmailAddress)
</div>
<div class="form-group">
@Html.LabelFor(s => s.StId)
@Html.DropDownListFor(s => s.StId, new SelectList(Model.States, "StId", "StAbbr"), "", new { @class = "form-control" })
@Html.ValidationMessageFor(s => s.StId)
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustDisplayName)
@Html.TextBoxFor(c => c.CustDisplayName, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustDisplayName)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustFirstName)
@Html.TextBoxFor(c => c.CustFirstName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustLastName)
@Html.TextBoxFor(c => c.CustLastName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustCompanyName)
@Html.TextBoxFor(c => c.CustCompanyName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustAddress)
@Html.TextBoxFor(c => c.CustAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustAddress)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustPhoneNumber)
@Html.TextBoxFor(c => c.CustPhoneNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustPhoneNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustMobileNumber)
@Html.TextBoxFor(c => c.CustMobileNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustMobileNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustEmailAddress)
@Html.TextBoxFor(c => c.CustEmailAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustEmailAddress)
</div>
<div class="form-group">
@Html.LabelFor(s => s.StId)
@Html.DropDownListFor(s => s.StId, new SelectList(Model.States, "StId", "StAbbr"), "", new { @class = "form-control" })
@Html.ValidationMessageFor(s => s.StId)
</div>
@Html.HiddenFor(c => c.CustId)
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
CustomPerformViewModel
public class CustomerFormViewModel
{
public int CustId { get; set; }
[Required(ErrorMessage = "Display Name is required!")]
[Display(Name = "Display Name")]
[StringLength(100)]
public string CustDisplayName { get; set; }
[Display(Name = "First Name")]
[StringLength(50)]
public string CustFirstName { get; set; }
[Display(Name = "Last Name")]
[StringLength(50)]
public string CustLastName { get; set; }
[Display(Name = "Company Name")]
[StringLength(50)]
public string CustCompanyName { get; set; }
[Display(Name = "Phone Number")]
[DataType(DataType.PhoneNumber)]
[StringLength(12)]
[RegularExpression(@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}", ErrorMessage = "Invalid Phone Number format!")]
public string CustPhoneNumber { get; set; }
[Display(Name = "Mobile Number")]
[DataType(DataType.PhoneNumber)]
[StringLength(12)]
[RegularExpression(@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}", ErrorMessage = "Invalid Number!")]
public string CustMobileNumber { get; set; }
[Display(Name = "Email Address")]
[DataType(DataType.EmailAddress)]
[StringLength(320)]
public string CustEmailAddress { get; set; }
[Required(ErrorMessage = "Address is required!")]
[Display(Name = "Address")]
[StringLength(100)]
public string CustAddress { get; set; }
[Required(ErrorMessage = "State is required!")]
[Display(Name = "State")]
public int StId { get; set; }
public IEnumerable<State> States { get; set; }
}
公共类CustomPerformViewModel
{
public int CustId{get;set;}
[必需(ErrorMessage=“需要显示名称!”)]
[显示(Name=“显示名称”)]
[长度(100)]
公共字符串CustDisplayName{get;set;}
[显示(Name=“First Name”)]
[长度(50)]
公共字符串CustFirstName{get;set;}
[显示(Name=“Last Name”)]
[长度(50)]
公共字符串CustLastName{get;set;}
[显示(名称=“公司名称”)]
[长度(50)]
公共字符串CustCompanyName{get;set;}
[显示(Name=“电话号码”)]
[数据类型(数据类型.电话号码)]
[条次建议修正案(12)]
[RegularExpression(@“(\(\d{3}\)?)|(\d{3}-)?\d{3}-\d{4}”,ErrorMessage=“电话号码格式无效!”)
公共字符串CustPhoneNumber{get;set;}
[显示(Name=“手机号码”)]
[数据类型(数据类型.电话号码)]
[条次建议修正案(12)]
[RegularExpression(@“(\(\d{3}\)?)|(\d{3}-)?\d{3}-\d{4}”,ErrorMessage=“无效数字!”)
公共字符串CustMobileNumber{get;set;}
[显示(Name=“电子邮件地址”)]
[数据类型(数据类型.电子邮件地址)]
[长度(320)]
公共字符串CustEmailAddress{get;set;}
[必需(ErrorMessage=“地址是必需的!”)]
[显示(Name=“Address”)]
[长度(100)]
公共字符串地址{get;set;}
[必需(ErrorMessage=“状态是必需的!”)]
[显示(Name=“State”)]
公共int StId{get;set;}
公共IEnumerable状态{get;set;}
}
客户控制器:
public class Customer
{
public int CustId { get; set; }
public string CustDisplayName { get; set; }
public string CustFirstName { get; set; }
public string CustLastName { get; set; }
public string CustCompanyName { get; set; }
public string CustAddress { get; set; }
public string CustPhoneNumber { get; set; }
public string CustMobileNumber { get; set; }
public string CustEmailAddress { get; set; }
public int StId { get; set; }
public State State { get; set; }
}
public class State
{
public int StId { get; set; }
public string StAbbr { get; set; }
public List<Customer> Customers { get; set; }
}
public class Manufacturer
{
public int MfrId { get; set; }
public string MfrCompanyName { get; set; }
public string MfrWebsiteDomainName { get; set; }
}
public class CustomerController : Controller
{
private WebAppDbContext _context;
public CustomerController(WebAppDbContext context)
{
_context = context;
}
// GET: /<Customer>/
public IActionResult Index()
{
return View(_context.Customers.ToList());
}
public ActionResult Create()
{
var states = _context.States.ToList();
var viewModel = new CustomerFormViewModel
{
States = states
};
return View(viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CustomerFormViewModel vm)
{
if (ModelState.IsValid)
{
var customer = new Customer();
{
customer.CustDisplayName = vm.CustDisplayName;
customer.CustFirstName = vm.CustFirstName;
customer.CustLastName = vm.CustLastName;
customer.CustCompanyName = vm.CustCompanyName;
customer.CustAddress = vm.CustAddress;
customer.CustPhoneNumber = vm.CustPhoneNumber;
customer.CustMobileNumber = vm.CustMobileNumber;
customer.CustEmailAddress = vm.CustEmailAddress;
customer.StId = vm.StId;
}
_context.Customers.Add(customer);
_context.SaveChanges();
return RedirectToAction("Index");
}
else
{
vm.States = _context.States.ToList();
return View(vm);
}
}
public ActionResult Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var customervm = new CustomerFormViewModel();
{
Customer customer = _context.Customers.SingleOrDefault(c => c.CustId == id);
if (customer == null)
{
return NotFound();
}
customervm.CustId = customer.CustId;
customervm.CustDisplayName = customer.CustDisplayName;
customervm.CustFirstName = customer.CustFirstName;
customervm.CustLastName = customer.CustLastName;
customervm.CustCompanyName = customer.CustCompanyName;
customervm.CustAddress = customer.CustAddress;
customervm.CustPhoneNumber = customer.CustPhoneNumber;
customervm.CustMobileNumber = customer.CustMobileNumber;
customervm.CustEmailAddress = customer.CustEmailAddress;
customervm.StId = customer.StId;
}
return View(customervm);
}
}
<div class="form-group">
@Html.LabelFor(c => c.CustDisplayName)
@Html.TextBoxFor(c => c.CustDisplayName, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustDisplayName)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustFirstName)
@Html.TextBoxFor(c => c.CustFirstName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustLastName)
@Html.TextBoxFor(c => c.CustLastName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustCompanyName)
@Html.TextBoxFor(c => c.CustCompanyName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustAddress)
@Html.TextBoxFor(c => c.CustAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustAddress)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustPhoneNumber)
@Html.TextBoxFor(c => c.CustPhoneNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustPhoneNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustMobileNumber)
@Html.TextBoxFor(c => c.CustMobileNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustMobileNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustEmailAddress)
@Html.TextBoxFor(c => c.CustEmailAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustEmailAddress)
</div>
<div class="form-group">
@Html.LabelFor(s => s.StId)
@Html.DropDownListFor(s => s.StId, new SelectList(Model.States, "StId", "StAbbr"), "", new { @class = "form-control" })
@Html.ValidationMessageFor(s => s.StId)
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustDisplayName)
@Html.TextBoxFor(c => c.CustDisplayName, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustDisplayName)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustFirstName)
@Html.TextBoxFor(c => c.CustFirstName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustLastName)
@Html.TextBoxFor(c => c.CustLastName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustCompanyName)
@Html.TextBoxFor(c => c.CustCompanyName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustAddress)
@Html.TextBoxFor(c => c.CustAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustAddress)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustPhoneNumber)
@Html.TextBoxFor(c => c.CustPhoneNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustPhoneNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustMobileNumber)
@Html.TextBoxFor(c => c.CustMobileNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustMobileNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustEmailAddress)
@Html.TextBoxFor(c => c.CustEmailAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustEmailAddress)
</div>
<div class="form-group">
@Html.LabelFor(s => s.StId)
@Html.DropDownListFor(s => s.StId, new SelectList(Model.States, "StId", "StAbbr"), "", new { @class = "form-control" })
@Html.ValidationMessageFor(s => s.StId)
</div>
@Html.HiddenFor(c => c.CustId)
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
公共类CustomerController:控制器
{
私有WebAppDbContext _context;
公共CustomerController(WebAppDbContext上下文)
{
_上下文=上下文;
}
//获取://
公共IActionResult索引()
{
返回视图(_context.Customers.ToList());
}
公共操作结果创建()
{
var states=_context.states.ToList();
var viewModel=新的CustomPerformViewModel
{
状态=状态
};
返回视图(viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果创建(CustomPerformViewModel虚拟机)
{
if(ModelState.IsValid)
{
var customer=新客户();
{
customer.CustDisplayName=vm.CustDisplayName;
customer.CustFirstName=vm.CustFirstName;
customer.CustLastName=vm.CustLastName;
customer.CustCompanyName=vm.CustCompanyName;
customer.CustAddress=vm.CustAddress;
customer.CustPhoneNumber=vm.CustPhoneNumber;
customer.CustMobileNumber=vm.CustMobileNumber;
customer.CustEmailAddress=vm.CustEmailAddress;
customer.StId=vm.StId;
}
_context.Customers.Add(客户);
_SaveChanges();
返回操作(“索引”);
}
其他的
{
vm.States=_context.States.ToList();
返回视图(vm);
}
}
公共行动结果编辑(int?id)
{
if(id==null)
{
返回NotFound();
}
var customervm=新的CustomerFormViewModel();
{
Customer=\u context.Customers.SingleOrDefault(c=>c.CustId==id);
如果(客户==null)
{
返回NotFound();
}
customervm.CustId=customer.CustId;
customervm.CustDisplayName=customer.CustDisplayName;
customervm.CustFirstName=customer.CustFirstName;
customervm.CustLastName=customer.CustLastName;
customervm.CustCompanyName=customer.CustCompanyName;
customervm.CustAddress=customer.CustAddress;
customervm.CustPhoneNumber=customer.CustPhoneNumber;
customervm.CustMobileNumber=customer.CustMobileNumber;
customervm.custmailaddress=customer.custmailaddress;
customervm.StId=customer.StId;
}
返回视图(customervm);
}
}
创建视图:
public class Customer
{
public int CustId { get; set; }
public string CustDisplayName { get; set; }
public string CustFirstName { get; set; }
public string CustLastName { get; set; }
public string CustCompanyName { get; set; }
public string CustAddress { get; set; }
public string CustPhoneNumber { get; set; }
public string CustMobileNumber { get; set; }
public string CustEmailAddress { get; set; }
public int StId { get; set; }
public State State { get; set; }
}
public class State
{
public int StId { get; set; }
public string StAbbr { get; set; }
public List<Customer> Customers { get; set; }
}
public class Manufacturer
{
public int MfrId { get; set; }
public string MfrCompanyName { get; set; }
public string MfrWebsiteDomainName { get; set; }
}
public class CustomerController : Controller
{
private WebAppDbContext _context;
public CustomerController(WebAppDbContext context)
{
_context = context;
}
// GET: /<Customer>/
public IActionResult Index()
{
return View(_context.Customers.ToList());
}
public ActionResult Create()
{
var states = _context.States.ToList();
var viewModel = new CustomerFormViewModel
{
States = states
};
return View(viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CustomerFormViewModel vm)
{
if (ModelState.IsValid)
{
var customer = new Customer();
{
customer.CustDisplayName = vm.CustDisplayName;
customer.CustFirstName = vm.CustFirstName;
customer.CustLastName = vm.CustLastName;
customer.CustCompanyName = vm.CustCompanyName;
customer.CustAddress = vm.CustAddress;
customer.CustPhoneNumber = vm.CustPhoneNumber;
customer.CustMobileNumber = vm.CustMobileNumber;
customer.CustEmailAddress = vm.CustEmailAddress;
customer.StId = vm.StId;
}
_context.Customers.Add(customer);
_context.SaveChanges();
return RedirectToAction("Index");
}
else
{
vm.States = _context.States.ToList();
return View(vm);
}
}
public ActionResult Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var customervm = new CustomerFormViewModel();
{
Customer customer = _context.Customers.SingleOrDefault(c => c.CustId == id);
if (customer == null)
{
return NotFound();
}
customervm.CustId = customer.CustId;
customervm.CustDisplayName = customer.CustDisplayName;
customervm.CustFirstName = customer.CustFirstName;
customervm.CustLastName = customer.CustLastName;
customervm.CustCompanyName = customer.CustCompanyName;
customervm.CustAddress = customer.CustAddress;
customervm.CustPhoneNumber = customer.CustPhoneNumber;
customervm.CustMobileNumber = customer.CustMobileNumber;
customervm.CustEmailAddress = customer.CustEmailAddress;
customervm.StId = customer.StId;
}
return View(customervm);
}
}
<div class="form-group">
@Html.LabelFor(c => c.CustDisplayName)
@Html.TextBoxFor(c => c.CustDisplayName, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustDisplayName)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustFirstName)
@Html.TextBoxFor(c => c.CustFirstName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustLastName)
@Html.TextBoxFor(c => c.CustLastName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustCompanyName)
@Html.TextBoxFor(c => c.CustCompanyName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustAddress)
@Html.TextBoxFor(c => c.CustAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustAddress)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustPhoneNumber)
@Html.TextBoxFor(c => c.CustPhoneNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustPhoneNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustMobileNumber)
@Html.TextBoxFor(c => c.CustMobileNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustMobileNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustEmailAddress)
@Html.TextBoxFor(c => c.CustEmailAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustEmailAddress)
</div>
<div class="form-group">
@Html.LabelFor(s => s.StId)
@Html.DropDownListFor(s => s.StId, new SelectList(Model.States, "StId", "StAbbr"), "", new { @class = "form-control" })
@Html.ValidationMessageFor(s => s.StId)
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustDisplayName)
@Html.TextBoxFor(c => c.CustDisplayName, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustDisplayName)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustFirstName)
@Html.TextBoxFor(c => c.CustFirstName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustLastName)
@Html.TextBoxFor(c => c.CustLastName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustCompanyName)
@Html.TextBoxFor(c => c.CustCompanyName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustAddress)
@Html.TextBoxFor(c => c.CustAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustAddress)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustPhoneNumber)
@Html.TextBoxFor(c => c.CustPhoneNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustPhoneNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustMobileNumber)
@Html.TextBoxFor(c => c.CustMobileNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustMobileNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustEmailAddress)
@Html.TextBoxFor(c => c.CustEmailAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustEmailAddress)
</div>
<div class="form-group">
@Html.LabelFor(s => s.StId)
@Html.DropDownListFor(s => s.StId, new SelectList(Model.States, "StId", "StAbbr"), "", new { @class = "form-control" })
@Html.ValidationMessageFor(s => s.StId)
</div>
@Html.HiddenFor(c => c.CustId)
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
@LabelFor(c=>c.CustDisplayName)
@TextBoxFor(c=>c.CustDisplayName,新的{@class=“formcontrol”})
@Html.ValidationMessageFor(c=>c.CustDisplayName)
@LabelFor(c=>c.CustFirstName)
@TextBoxFor(c=>c.CustFirstName,新的{@class=“form control”})
@LabelFor(c=>c.CustLastName)
@TextBoxFor(c=>c.CustLastName,new{@class=“form control”})
@LabelFor(c=>c.CustCompanyName)
@TextBoxFor(c=>c.CustCompanyName,新的{@class=“form control”})
@LabelFor(c=>c.CustAddress)
@TextBoxFor(c=>c.CustAddress,新的{@class=“form control”})
@Html.ValidationMessageFor(c=>c.CustAddress)
@LabelFor(c=>c.CustPhoneNumber)
@TextBoxFor(c=>c.CustPhoneNumber,新的{@class=“form control”})
@Html.ValidationMessageFor(c=>c.CustPhoneNumber)
@LabelFor(c=>c.CustMobileNumber)
@TextBoxFor(c=>c.CustMobileNumber,新的{@class=“form control”})
@Html.ValidationMessageFor(c=>c.CustMobileNumber)
@LabelFor(c=>c.CustEmailAddress)
@TextBoxFor(c=>c.CustEmailAddress,新的{@class=“form control”})
@Html.ValidationMessageFor(c=>c.CustEmailAddress)
@LabelFor(s=>s.StId)
@DropDownListFor(s=>s.StId,new SelectList(Model.States,“StId”,“stirr”),“”,new{@class=“form control”})
@Html.ValidationMessageFor(s=>s.StId)
提交
编辑视图:
public class Customer
{
public int CustId { get; set; }
public string CustDisplayName { get; set; }
public string CustFirstName { get; set; }
public string CustLastName { get; set; }
public string CustCompanyName { get; set; }
public string CustAddress { get; set; }
public string CustPhoneNumber { get; set; }
public string CustMobileNumber { get; set; }
public string CustEmailAddress { get; set; }
public int StId { get; set; }
public State State { get; set; }
}
public class State
{
public int StId { get; set; }
public string StAbbr { get; set; }
public List<Customer> Customers { get; set; }
}
public class Manufacturer
{
public int MfrId { get; set; }
public string MfrCompanyName { get; set; }
public string MfrWebsiteDomainName { get; set; }
}
public class CustomerController : Controller
{
private WebAppDbContext _context;
public CustomerController(WebAppDbContext context)
{
_context = context;
}
// GET: /<Customer>/
public IActionResult Index()
{
return View(_context.Customers.ToList());
}
public ActionResult Create()
{
var states = _context.States.ToList();
var viewModel = new CustomerFormViewModel
{
States = states
};
return View(viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CustomerFormViewModel vm)
{
if (ModelState.IsValid)
{
var customer = new Customer();
{
customer.CustDisplayName = vm.CustDisplayName;
customer.CustFirstName = vm.CustFirstName;
customer.CustLastName = vm.CustLastName;
customer.CustCompanyName = vm.CustCompanyName;
customer.CustAddress = vm.CustAddress;
customer.CustPhoneNumber = vm.CustPhoneNumber;
customer.CustMobileNumber = vm.CustMobileNumber;
customer.CustEmailAddress = vm.CustEmailAddress;
customer.StId = vm.StId;
}
_context.Customers.Add(customer);
_context.SaveChanges();
return RedirectToAction("Index");
}
else
{
vm.States = _context.States.ToList();
return View(vm);
}
}
public ActionResult Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var customervm = new CustomerFormViewModel();
{
Customer customer = _context.Customers.SingleOrDefault(c => c.CustId == id);
if (customer == null)
{
return NotFound();
}
customervm.CustId = customer.CustId;
customervm.CustDisplayName = customer.CustDisplayName;
customervm.CustFirstName = customer.CustFirstName;
customervm.CustLastName = customer.CustLastName;
customervm.CustCompanyName = customer.CustCompanyName;
customervm.CustAddress = customer.CustAddress;
customervm.CustPhoneNumber = customer.CustPhoneNumber;
customervm.CustMobileNumber = customer.CustMobileNumber;
customervm.CustEmailAddress = customer.CustEmailAddress;
customervm.StId = customer.StId;
}
return View(customervm);
}
}
<div class="form-group">
@Html.LabelFor(c => c.CustDisplayName)
@Html.TextBoxFor(c => c.CustDisplayName, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustDisplayName)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustFirstName)
@Html.TextBoxFor(c => c.CustFirstName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustLastName)
@Html.TextBoxFor(c => c.CustLastName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustCompanyName)
@Html.TextBoxFor(c => c.CustCompanyName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustAddress)
@Html.TextBoxFor(c => c.CustAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustAddress)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustPhoneNumber)
@Html.TextBoxFor(c => c.CustPhoneNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustPhoneNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustMobileNumber)
@Html.TextBoxFor(c => c.CustMobileNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustMobileNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustEmailAddress)
@Html.TextBoxFor(c => c.CustEmailAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustEmailAddress)
</div>
<div class="form-group">
@Html.LabelFor(s => s.StId)
@Html.DropDownListFor(s => s.StId, new SelectList(Model.States, "StId", "StAbbr"), "", new { @class = "form-control" })
@Html.ValidationMessageFor(s => s.StId)
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustDisplayName)
@Html.TextBoxFor(c => c.CustDisplayName, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustDisplayName)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustFirstName)
@Html.TextBoxFor(c => c.CustFirstName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustLastName)
@Html.TextBoxFor(c => c.CustLastName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustCompanyName)
@Html.TextBoxFor(c => c.CustCompanyName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustAddress)
@Html.TextBoxFor(c => c.CustAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustAddress)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustPhoneNumber)
@Html.TextBoxFor(c => c.CustPhoneNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustPhoneNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustMobileNumber)
@Html.TextBoxFor(c => c.CustMobileNumber, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustMobileNumber)
</div>
<div class="form-group">
@Html.LabelFor(c => c.CustEmailAddress)
@Html.TextBoxFor(c => c.CustEmailAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.CustEmailAddress)
</div>
<div class="form-group">
@Html.LabelFor(s => s.StId)
@Html.DropDownListFor(s => s.StId, new SelectList(Model.States, "StId", "StAbbr"), "", new { @class = "form-control" })
@Html.ValidationMessageFor(s => s.StId)
</div>
@Html.HiddenFor(c => c.CustId)
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
@LabelFor(c=>c.custdepra