【问题标题】:Validation failed for one or more entities error一个或多个实体错误的验证失败
【发布时间】:2013-11-30 07:01:33
【问题描述】:

在保存客户端名称时,它显示 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);
}

【问题讨论】:

  • 当表单从您的“编辑”视图提交时..它是否命中您的 create[Post] ActionResult?
  • aritra :是的,它命中了我的 PostActionResult。
  • @Iliya.Sullimanov :它始终将 clientID 显示为零。可能是什么?

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


【解决方案1】:

知道了。 我只是弄错了参数。

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

【讨论】:

    猜你喜欢
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 2020-02-02
    • 1970-01-01
    相关资源
    最近更新 更多