C# 通过.net core上的模式添加/编辑记录
我一直在尝试通过在.NETCore中使用模式来创建一个能够添加/编辑记录的数据库 我一直在尝试一些教程,比如 这两种方法都不像.net那样有效。模态没有出现 ViewClients.cshtmlC# 通过.net core上的模式添加/编辑记录,c#,asp.net-core,C#,Asp.net Core,我一直在尝试通过在.NETCore中使用模式来创建一个能够添加/编辑记录的数据库 我一直在尝试一些教程,比如 这两种方法都不像.net那样有效。模态没有出现 ViewClients.cshtml 您是否在项目中创建了DbContext类 下面的代码只是用于显示数据和编辑或向表中添加数据的示例 [HttpGet] public IActionResult AddEditCustomer(long? id) { Cust
您是否在项目中创建了DbContext类 下面的代码只是用于显示数据和编辑或向表中添加数据的示例
[HttpGet]
public IActionResult AddEditCustomer(long? id)
{
CustomerViewModel model = new CustomerViewModel();
if (id.HasValue)
{
Customer customer = context.Set<Customer>().SingleOrDefault(c => c.Id == id.Value);
if (customer != null)
{
model.Id = customer.Id;
model.FirstName = customer.FirstName;
model.LastName = customer.LastName;
model.MobileNo = customer.MobileNo;
model.Email = customer.Email;
}
}
return PartialView("~/Views/Customer/_AddEditCustomer.cshtml", model);
}
[HttpPost]
public ActionResult AddEditCustomer(long? id, CustomerViewModel model)
{
try
{
if (ModelState.IsValid)
{
bool isNew = !id.HasValue;
Customer customer = isNew ? new Customer
{
AddedDate = DateTime.UtcNow
} : context.Set<Customer>().SingleOrDefault(s => s.Id == id.Value);
customer.FirstName = model.FirstName;
customer.LastName = model.LastName;
customer.MobileNo = model.MobileNo;
customer.Email = model.Email;
customer.IPAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();
customer.ModifiedDate = DateTime.UtcNow;
if (isNew)
{
context.Add(customer);
}
context.SaveChanges();
}
}
catch (Exception ex)
{
throw ex;
}
return RedirectToAction("Index");
}
[HttpGet]
公共IActionResult AddEditCustomer(长?id)
{
CustomerViewModel模型=新CustomerViewModel();
if(id.HasValue)
{
Customer=context.Set().SingleOrDefault(c=>c.Id==Id.Value);
如果(客户!=null)
{
model.Id=customer.Id;
model.FirstName=customer.FirstName;
model.LastName=customer.LastName;
model.MobileNo=customer.MobileNo;
model.Email=customer.Email;
}
}
返回PartialView(“~/Views/Customer/_AddEditCustomer.cshtml”,model);
}
[HttpPost]
public ActionResult AddEditCustomer(长?id,CustomServiceWModel模型)
{
尝试
{
if(ModelState.IsValid)
{
bool isNew=!id.HasValue;
客户=isNew?新客户
{
AddedDate=DateTime.UtcNow
}:context.Set().SingleOrDefault(s=>s.Id==Id.Value);
customer.FirstName=model.FirstName;
customer.LastName=model.LastName;
customer.MobileNo=model.MobileNo;
customer.Email=model.Email;
customer.IPAddress=Request.HttpContext.Connection.RemoteIpAddress.ToString();
customer.ModifiedDate=DateTime.UtcNow;
如果(是新的)
{
添加(客户);
}
SaveChanges();
}
}
捕获(例外情况除外)
{
掷骰子;
}
返回操作(“索引”);
}
您是否在项目中创建了DbContext类
下面的代码只是用于显示数据和编辑或向表中添加数据的示例
[HttpGet]
public IActionResult AddEditCustomer(long? id)
{
CustomerViewModel model = new CustomerViewModel();
if (id.HasValue)
{
Customer customer = context.Set<Customer>().SingleOrDefault(c => c.Id == id.Value);
if (customer != null)
{
model.Id = customer.Id;
model.FirstName = customer.FirstName;
model.LastName = customer.LastName;
model.MobileNo = customer.MobileNo;
model.Email = customer.Email;
}
}
return PartialView("~/Views/Customer/_AddEditCustomer.cshtml", model);
}
[HttpPost]
public ActionResult AddEditCustomer(long? id, CustomerViewModel model)
{
try
{
if (ModelState.IsValid)
{
bool isNew = !id.HasValue;
Customer customer = isNew ? new Customer
{
AddedDate = DateTime.UtcNow
} : context.Set<Customer>().SingleOrDefault(s => s.Id == id.Value);
customer.FirstName = model.FirstName;
customer.LastName = model.LastName;
customer.MobileNo = model.MobileNo;
customer.Email = model.Email;
customer.IPAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();
customer.ModifiedDate = DateTime.UtcNow;
if (isNew)
{
context.Add(customer);
}
context.SaveChanges();
}
}
catch (Exception ex)
{
throw ex;
}
return RedirectToAction("Index");
}
[HttpGet]
公共IActionResult AddEditCustomer(长?id)
{
CustomerViewModel模型=新CustomerViewModel();
if(id.HasValue)
{
Customer=context.Set().SingleOrDefault(c=>c.Id==Id.Value);
如果(客户!=null)
{
model.Id=customer.Id;
model.FirstName=customer.FirstName;
model.LastName=customer.LastName;
model.MobileNo=customer.MobileNo;
model.Email=customer.Email;
}
}
返回PartialView(“~/Views/Customer/_AddEditCustomer.cshtml”,model);
}
[HttpPost]
public ActionResult AddEditCustomer(长?id,CustomServiceWModel模型)
{
尝试
{
if(ModelState.IsValid)
{
bool isNew=!id.HasValue;
客户=isNew?新客户
{
AddedDate=DateTime.UtcNow
}:context.Set().SingleOrDefault(s=>s.Id==Id.Value);
customer.FirstName=model.FirstName;
customer.LastName=model.LastName;
customer.MobileNo=model.MobileNo;
customer.Email=model.Email;
customer.IPAddress=Request.HttpContext.Connection.RemoteIpAddress.ToString();
customer.ModifiedDate=DateTime.UtcNow;
如果(是新的)
{
添加(客户);
}
SaveChanges();
}
}
捕获(例外情况除外)
{
掷骰子;
}
返回操作(“索引”);
}
这是因为您缺少HTTP Post属性。请查看此处的文档
这是因为您缺少HTTP Post属性。请参阅此处的文档
是的,这不是问题所在。使用该方法返回部分视图而不是部分视图不是很晚。您可以在asp core mvc中使用ViewComponent。是的,这不是问题所在。返回部分视图而不是部分视图的方法还不是很晚。您可以在asp core mvc中使用ViewComponent。因为您没有用[HTTPPOST]修饰您的方法,所以必须修饰它,否则ajax将找不到MoethodBeach,因为您没有用[HTTPPOST]修饰您的方法它必须装饰,否则ajax将找不到moethod
public IActionResult EditClient(int ClientID)
{
Clients client = new Clients();
if(ClientID > 0)
{
var user = _context.ClientTable.SingleOrDefault(x => x.ClientID == clientID);
client.ClientID = user.ClientID;
client.ClientName = user.ClientName;
}
return PartialView("EditClient", client);
}
}
[HttpGet]
public IActionResult AddEditCustomer(long? id)
{
CustomerViewModel model = new CustomerViewModel();
if (id.HasValue)
{
Customer customer = context.Set<Customer>().SingleOrDefault(c => c.Id == id.Value);
if (customer != null)
{
model.Id = customer.Id;
model.FirstName = customer.FirstName;
model.LastName = customer.LastName;
model.MobileNo = customer.MobileNo;
model.Email = customer.Email;
}
}
return PartialView("~/Views/Customer/_AddEditCustomer.cshtml", model);
}
[HttpPost]
public ActionResult AddEditCustomer(long? id, CustomerViewModel model)
{
try
{
if (ModelState.IsValid)
{
bool isNew = !id.HasValue;
Customer customer = isNew ? new Customer
{
AddedDate = DateTime.UtcNow
} : context.Set<Customer>().SingleOrDefault(s => s.Id == id.Value);
customer.FirstName = model.FirstName;
customer.LastName = model.LastName;
customer.MobileNo = model.MobileNo;
customer.Email = model.Email;
customer.IPAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();
customer.ModifiedDate = DateTime.UtcNow;
if (isNew)
{
context.Add(customer);
}
context.SaveChanges();
}
}
catch (Exception ex)
{
throw ex;
}
return RedirectToAction("Index");
}
[HTTPPOST]
public IActionResult EditClient(int ClientID)
{
Clients client = new Clients();
if(ClientID > 0)
{
var user = _context.ClientTable.SingleOrDefault(x => x.ClientID == clientID);
client.ClientID = user.ClientID;
client.ClientName = user.ClientName;
}
return PartialView("EditClient", client);
}
}