I'm new to MVC and so far I've connected my database and created a entity controller class with the default CRUD.
However, in my Index.cshtml
I'm trying to update the database with an ajax script but I'm getting an error and I don't know where to go from there.
Database : MyDB
Users Table
==========================================
| Column | Type | |
==========================================
| ID | int | Primary AI |
------------------------------------------
| Name | varchar (50) | NULL |
------------------------------------------
| Company | varchar (50 | NULL |
==========================================
[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);
}
<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>
Error I'm getting is [Object object] error not found
Your problem is the parameter in your controller action:
public ActionResult Create([Bind(Include = "ID,Name,Company")] MyDB myDB)
You are trying to POST
a User
to a controller that takes MyDB
which I'm guessing is your DBContext. It should be:
public ActionResult Create([Bind(Include = "ID,Name,Company")] User user)
Replace User
with whatever your user class actually is
Your POST method is decorated with the [ValidateAntiForgeryToken]
attribute but your ajax call does not pass the token so the method is never run..
Either remove the attribute, or if you have a form that includes @Html.AntiForgeryToken()
, you can pass the token using
var token = $('[name=__RequestVerificationToken]').val();
$.ajax({
....
data: { 'Name': $name, 'Company': $company, '__RequestVerificationToken': token },
or better, assuming you have form controls for Name
and Company
, just use
data: $('form').serialize(),
which will serialize all the form data including the token.
In addition, as drneel as noted in the other answer, your model appears to be User
, not MyDB
so it would be
public ActionResult Create(User user)
Side note; It appears you want all properties of you model, so your [Bind(Include = "..")]
attribute is a bit pointless (all properties are included by default). Having said that, an ID
(primary key) property is not appropriate in a 'Create' method and a malicious user could easily post back an ID
value, causing your method to fail. You should be using a view model containing only the Name
and 'Company' properties, then map the view model properties to a new instance of the data model and save the data model. I also suggest that the Name
property should be NOT NULL
(and possibly the Company
as well)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.