I'm developing an application that should be able to create a new object,my method must be able to accept data entered. I'm having a problem on adding code to assign a new guid value to my id property and initializing the service property for every new car class object.
My controller code:
[HttpPost]
Public ActionResult Create(Guid?Id,Car model)
{
If(ModelState.IsValid)
{
bookingList=GetBookings();
model.Id=bookingList.Count+1;
bookingList.Add(model);
TempData["bookingList"]= bookingList;
return RedirectToAction("Index");
}
return View(model);
}
If I understand your question correctly, you want to assign a new guid to your Model.Id
field. And as per your code it seems you're passing guid in request ie Guid? Id
Guid? Id
.
Assuming above, You can try the following code:
[HttpPost]
Public ActionResult Create(Guid? Id, Car model)
{
If(ModelState.IsValid)
{
bookingList=GetBookings();
model.Id= Id ?? Guid.NewGuid();
bookingList.Add(model);
TempData["bookingList"]= bookingList;
return RedirectToAction("Index");
}
return View(model);
}
}
Here the line model.Id= Id ?? Guid.NewGuid();
model.Id= Id ?? Guid.NewGuid();
line will assign the Id which you've passed in request otherwise it'll assign new GUID.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.