Adding a new object,form creation

时间:2018-05-06 17:09:46

标签: c# asp.net-mvc

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:

$('#Id')

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你想为你的Model.Id字段分配一个新的guid。根据你的代码,似乎你在请求中传递了guid,Guid? Id

假设如上所述,您可以尝试以下代码:

[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);
    }
}

此处行model.Id= Id ?? Guid.NewGuid();行将指定您在请求中传递的ID,否则它将分配新的GUID。