Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc MVC无法将数据保存到数据库_Asp.net Mvc - Fatal编程技术网

Asp.net mvc MVC无法将数据保存到数据库

Asp.net mvc MVC无法将数据保存到数据库,asp.net-mvc,Asp.net Mvc,我正在尝试使用post将数据保存到从视图传递的数据库中。 我没有得到任何错误,但它没有将值添加到数据库中 我的json看起来像 { "Schedule Name": "name1", "data1": "a3c1", "data2": "a3c1", "data3": "a3c1", "data4": "a3c1", "data5": "a3c1", "data6": "a3c1", "data7": "a3c1" } 这是我的控

我正在尝试使用post将数据保存到从视图传递的数据库中。 我没有得到任何错误,但它没有将值添加到数据库中

我的json看起来像

{
    "Schedule Name": "name1",
    "data1": "a3c1",
    "data2": "a3c1",
    "data3": "a3c1",
    "data4": "a3c1",
    "data5": "a3c1",
    "data6": "a3c1",
    "data7": "a3c1"
}
这是我的控制器

public class ScheduleController : Controller
{
    DatabaseContext _db = new DatabaseContext();

    public ActionResult schedule()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Addmore(string list1)
    {
        bool result = true;
        newlist personData;
        JavaScriptSerializer jss = new JavaScriptSerializer();
        personData = jss.Deserialize<newlist>(list1);
        savv(personData);
        return Content(result.ToString());
    }

    public void savv(newlist nl) 
    {
        if (ModelState.IsValid)
        {
            using (_db)
            {
                _db.Schedule.Add(nl);
                _db.SaveChanges();
                ModelState.Clear();
            }
        }
    }

    public class newlist
    {
        public string namIDs { get; set; }
        public string aIDs { get; set; }
        public string bIDs { get; set; }
        public string cIDs { get; set; }
        public string dIDs { get; set; }
        public string eIDs { get; set; }
        public string fIDs { get; set; }
        public string gIDs { get; set; }
    }
}
文本状态始终返回“success”,但没有值存储到数据库中 有人能帮我吗?我在代码上遗漏了什么

我只是使用另一种方法将值存储到数据库中

public ActionResult Addmore(newlist personData)
    {
        var data1 = "hello";
        bool result = false;
        try
        {
            con.Open();
            //SqlCommand cmd = new SqlCommand("Insert INTO Sche values('" + personData.namIDs + "','" + personData.aIDs + "','" + personData.bIDs + "','" + personData.cIDs + "','" + personData.dIDs + "','" + personData.eIDs + "','" + personData.fIDs + "','" + personData.gIDs + "')", con);
            SqlCommand cmd = new SqlCommand("INSERT INTO Sche VALUES('" + data1 + "','" + data1 + "','" + data1 + "','" + data1 + "','" + data1 + "','" + data1 + "','" + data1 + "','" + data1 + "')", con);

            int i = cmd.ExecuteNonQuery();
            con.Close();
            if (i > 0) { result = true; }

        }
        catch (Exception e)
        {
            result = false;
        }
        return Content(result.ToString());
    }
但这对我来说仍然不起作用

即使我在代码上输入了这个

SqlCommand cmd = new SqlCommand("INSERT INTO Sche VALUES(1,2,3,4,5,6,7,8)", con);
但它仍然没有添加到数据库中 这不是连接错误,因为此命令行可以工作

string query=“SELECT*FROM Sche”;
SqlCommand=newsqlcommand(查询、连接)

操作的参数名和post参数名必须相同。将
jsonData
更改为
list1
,反之亦然

[HttpPost]
public ActionResult Addmore(newlist personData)
{
    bool result = true;
    JavaScriptSerializer jss = new JavaScriptSerializer();

    savv(personData);
    return Content(result.ToString());
}
阿贾克斯:


就我个人而言,我不会使用MVC控制器和字符串化json,因为它会改变数据类型。按原样发布数据,并改用web API。它有一个内置的机制来解析JSON数据

你有太多的错误,很难知道从哪里开始。首先将您的方法更改为
public ActionResult Addmore(newlist model)
。在ajax中,使用
数据:{namIDs:'abc',aIDs:'def',cIDs:'ghi',…..etc},
-即匹配模型属性。
model
参数将与您发布的正确值绑定如果只是发布数据,那么控制器如何接收数据?最好使用web API控制器。您知道如何创建web API控制器吗?如果没有,请参考以下链接MVC还将直接将JSON数据解析到模型中!
SqlCommand cmd = new SqlCommand("INSERT INTO Sche VALUES(1,2,3,4,5,6,7,8)", con);
[HttpPost]
public ActionResult Addmore(newlist personData)
{
    bool result = true;
    JavaScriptSerializer jss = new JavaScriptSerializer();

    savv(personData);
    return Content(result.ToString());
}
$.ajax({
    url: '@Url.Action("Addmore", "Schedule")',
    data: JSON.stringify({ namIDs : 'def' ,aIDs:'data1' ,bIDs :'data2',cIDs :'data3',dIDs :'data4',eIDs :'data5',fIDs :'data6',gIDs:'data7'}),
    dataType: 'json',
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        }
});