首先,我对MVC很新。
MyDB
的SQLEXPRESS数据库和一个名为Users
的表,同时ID
列设置为Primary AutoIncrement
我在我的Dababase连接的Models文件夹中创建了一个类
namespace WebApplication1.Models
{
public class MyDB
{
public int ID { get; set; }
public string Name { get; set; }
public string Company { get; set; }
}
public class MyDBContext : DbContext
{
public DbSet<MyDB> Users { get; set; }
}
}
构建解决方案后,我右键单击Controllers
并创建了一个新控制器。
脚手架后,我留下了这个:
namespace WebApplication1.Controllers
{
public class MyDBsController : Controller
{
private MyDBContext db = new MyDBContext();
// GET: MyDB
public ActionResult Index()
{
return View(db.Users.ToList());
}
// GET: MyDBs/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
MyDB myDB = db.Users.Find(id);
if (myDB == null)
{
return HttpNotFound();
}
return View(myDB);
}
// GET: MyDBs/Create
public ActionResult Create()
{
return View();
}
// POST: MyDBs/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Name,Company")] MyDB myDB)
{
if (ModelState.IsValid)
{
db.Users.Add(myDB);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(myDB);
}
// GET: MyDBs/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
MyDB myDB = db.Users.Find(id);
if (myDB == null)
{
return HttpNotFound();
}
return View(myDB);
}
// POST: MyDBs/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,Name,Company")] MyDB myDB)
{
if (ModelState.IsValid)
{
db.Entry(myDB).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(myDB);
}
// GET: MyDBs/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
MyDB myDB = db.Users.Find(id);
if (myDB == null)
{
return HttpNotFound();
}
return View(myDB);
}
// POST: MyDBs/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
MyDB myDB = db.Users.Find(id);
db.Users.Remove(myDB);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
我复制并粘贴整个块,所以请原谅任何不匹配的括号,我尝试了(几乎)最好的。
现在在Index.cshtml
文件夹下的Home
我有一个脚本:
<script>
$name = "John";
$company = "123Moving";
</script>
如何在我的数据库中获取这些值?
<script>
$('a').click(function () {
$name = "John";
$company = "123Moving";
$.ajax({
url: '@Url.Action("Create", "MyDBsController")',
data: { 'Name': $name, 'Company' : $company },
type: "post",
cache: false,
success: function () {
alert("Success");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr + ajaxOptions + thrownError);
}
})
})
</script>
我得到的错误是[Object object] error not found
答案 0 :(得分:1)
这是一个基本的例子。我删除了您稍后可以添加的ValidateAntiForgeryToken
。你正在做一个AJAX帖子,所以返回一个完整的视图并不是你想要的。而是返回一个html片段(部分视图)。
public class MyDBsController : Controller
{
[HttpPost]
public ActionResult Create(MyDB myDB)
{
// do stuff...
return PartialView("Success", myDB);
}
}
<强> Success.cshtml 强>
@model MyDB
<h2>Success</h2>
<p>@Model.Name Created!</p>
设置div占位符以报告结果。
<强> Index.cshtml 强>
<a href="#">Create</a>
<div id="result"></div>
您的AJAX请求是从锚标记触发的,因此您必须禁用默认行为。 ControllerName
不应包含“控制器”。
$('a').click(function (event) {
event.preventDefault();
var $name = "John";
var $company = "123Moving";
$.ajax({
url: "@Url.Action("Create", "MyDBs")",
data: { "Name": $name, "Company" : $company },
type: "post",
success: function (partialViewResult) {
// insert response into the div placeholder
$("#result").html(partialViewResult);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr, ajaxOptions, thrownError);
}
});
});
而不是alert()
使用console.log()
通过浏览器的调试器获得更好的细节。