最近初步接触了MVC5,想自行设计一个学生信息管理系统,但是遇到一个问题:通过网页输入一个ViewModel的全部属性之后,怎么将他通过JQuery Ajax的Post()方法提交到数据库
下面是我的StudentViewModel:
public class Student
{
[Display(Name = "学号")]
public int StdId { get; set; }
[Display(Name = "姓名")]
public string StdName { get; set; }
[Display(Name = "性别")]
public string StdSex { get; set; }
[Display(Name = "年龄")]
public int StdAge { get; set; }
}
接着是StudentDataOperatin的Create方法:
public void Create(Student t)
{
IDbConnection con = new SqlConnection(this.Sqlcon);
IDbCommand cmd = new SqlCommand(@"Insert into Student (StdId,StdName,StdSex,StdAge)Values(@StdId,@StdName,@StdSex,@StdAge);");
cmd.Connection = con;
cmd.Parameters.Add(new SqlParameter("@StdId", t.StdId));
cmd.Parameters.Add(new SqlParameter("@StdName", t.StdName));
cmd.Parameters.Add(new SqlParameter("@StdSex", t.StdSex));
cmd.Parameters.Add(new SqlParameter("@StdAge", t.StdAge));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
接着是Action:
public ActionResult Create2()
{
return View(new Student() { StdId = 1 });
}
[HttpPost]
public ActionResult Create2(Student t)
{
try
{
// TODO: Add insert logic here
studentDataOperation.Create(t);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
然后就是View:
@model MVC2.Models.ViewModels.Student
@{
ViewBag.Title = "Create2";
}
Create2
学号:
姓名:
性别:
年龄:
添加
各位路过的神仙能帮忙解决下不?

:至于属性名Std*,是因为入门语言c++,写 using namespace std;习惯之后敲上去了,后来嫌麻烦就不改了。。。。。