一个或多个实体错误的验证失败

时间:2013-11-30 07:01:33

标签: entity-framework asp.net-mvc-4 c#-4.0 ef-code-first entity-framework-5

保存客户端名称时,它显示clientID的值为null。 我尝试了很多,但无法得到解决方案。 这是代码:

控制器:

[SpecialAuthorize("Client_Add")]
public ActionResult Create()
{
    var client = new OData.Client();
    client.Picture = Guid.NewGuid().ToString();
    SetMyBag(client);
    return View("Edit", client);
}

[HttpPost]
[SpecialAuthorize("Client_Add")]
public ActionResult Create(OData.Client model)
{
    var client = new OData.Client();
    if (ModelState.IsValid == true)
    {
        db.Clients.Add(client);
        db.SaveChanges();
        db.LogHistory(new Activity(ActionEnum.Created, model.ClientId, model.ResellerName, model.ClientId, "Client"));
        return RedirectToAction("Index");
    }
    SetMyBag(client);
    return View("Edit", model);
}

1 个答案:

答案 0 :(得分:0)

知道了。 我只是错误地把参数弄错了。

         [SpecialAuthorize("Client_Add")]
     public ActionResult Create(Model.Client model)
     {
       //var client = new Model.Client();
         if (ModelState.IsValid)
         {
            db.Clients.Add(model);
             db.SaveChanges();
             db.LogHistory(new Activity(ActionEnum.Created, model.ClientId, model.ResellerName, model.ClientId, "Client"));
             return RedirectToAction("Index");
         }
        SetMyBag(model);
         return View("Edit", model);
     }