名称模型状态在这种情况下似乎不存在-错误

时间:2018-07-20 07:56:43

标签: c# asp.net asp.net-mvc visual-studio entity-framework-6

我试图将JSON对象传递给控制器​​操作方法,然后使用模型将更改映射到数据库。 我的构建成功,但是出现以下错误。需要帮忙!!!事实证明是一个时间杀手。

控制器操作方法:

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Updateto(GitJSON gitjson)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //I need to map entites regarding each list item and update
                    //  var gitdIds = gitjson.gitdList.Select(x => x.ID).ToList;
                    var gitds = _context.gitd;//the table and the database context
                   // .Where(x => gitdIds.Contains(x.ID))//the names in the model class:ID,NAME etc
                   // .ToList();
                    foreach (gitd value in gitjson.gitdList)
                    {
                        Console.WriteLine(value);
                    }



                    //save changes to database
                    await _context.SaveChangesAsync();
                }
                return Ok();
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    error = ex.Message// or ex.ToString()
                });

                // or return RedirectToPage("Error", ex);
            }
        }

控制器:

namespace Github.Controllers
{
    public class Gitcontroller : Controller
    {
        private readonly GithubContext _context;

    public Gitcontroller(GithubContext context)//the database context
    {
        _context = context;
    }
   }
}

型号:

namespace Github.Models
{
    public class gitd
    {
        public int ID { get; set; }
        public string AvatarURL { get; set; }
        public string Name { get; set; }
        public decimal Score { get; set; }
        public DateTime Updatedat { get; set; }


}
public class GitJSON
{
    public List<gitd> gitdList { set; get; }
}

}

错误:

(1,5): error CS0103: The name 'ModelState' does not exist in the current context
(5,33): error CS0103: The name '_context' does not exist in the current context
(8,30): error CS0246: The type or namespace name 'gitd' could not be found (are you missing a using directive or an assembly reference?)
(8,44): error CS0103: The name 'gitjson' does not exist in the current context
(16,27): error CS0103: The name '_context' does not exist in the current context

我正在尝试将JSON对象保存到数据库中。需要朝着正确的方向推进谢谢。好像我一直在向垃圾邮件泛滥,永远有问题:P。

0 个答案:

没有答案