我正在使用VS 2015,我創建了一個ASP.NET MVC項目,並添加了一些事情(登錄和註冊確實工作正常與Localdb調用MyDatabase.mdf
),確實工作。ASP.NET MVC CRUD與LocalDb savechanges錯誤
CRUD僅在db中創建函數,其餘部分將在以後出現。
但現在我無法得到這項工作。裏面的LocalDB稱爲MyDatabase.mdf
,我已經創建另一個表名爲Amsterdam
:
[MyDatabase.mdf] [1]
這是我MainDbContext.cs
- 在這裏我加
public DbSet<Amsterdam> Amsterdam { get; set; }
在我Home
我寫的Amsterdam.cshtml
這樣:
@model IEnumerable<MyFirstWebsite.Models.Amsterdam>
@{
ViewBag.Title = "Amsterdam";
var username = User.Identity.Name;
}
<h2>@username's Bestellijst Amsterdam</h2>
@using (Html.BeginForm())
{
<span>Enter new item: </span>
<br/>
<input type="text" name="new_item"/>
<br/>
<span>Public post?</span>
<input type="checkbox" name="check_public"/><br/>
<br/>
<input type="submit" value="Add Item"/>
}
<br/>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th style="text-align: center;">Id Bestelling</th>
<th style="text-align: center;">Details Bestelling</th>
<th style="text-align: center;">Time - Ontvangen Bestelling</th>
<th style="text-align: center;">Time - Verzonden Bestelling</th>
<th style="text-align: center;">Edit</th>
<th style="text-align: center;">Delete</th>
<th style="text-align: center;">Public Post</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center;"></td>
<td style="text-align: center;"></td>
<td style="text-align: center;"></td>
<td style="text-align: center;"></td>
<td style="text-align: center;">
<a href="@Url.Action("Edit", "Home")/">Edit</a>
</td>
<td style="text-align: center;">
<a href="@Url.Action("Delete", "Home")/">Edit</a>
</td>
<td style="text-align: center;"></td>
</tr>
</tbody>
</table>
而在我的Models
文件夾我創建Amsterdam.cs
,看起來像這樣:
namespace MyFirstWebsite.Models
{
public class Amsterdam
{
[Key]
public int Id { get; set; }
public string Details { get; set; }
public string Date_Posted { get; set; }
public string Time_Posted { get; set; }
public string Date_Edited { get; set; }
public string Time_Edited { get; set; }
public string Public { get; set; }
public int User_Id { get; set; }
}
}
在我Controllers
文件夾我的HomeController
看起來是這樣的:
public ActionResult Amsterdam()
{
return View();
}
[HttpPost]
public ActionResult Amsterdam(Amsterdam list)
{
string timeToday = DateTime.Now.ToString("h:mm:ss tt");
string dateToday = DateTime.Now.ToString("M/dd/yyyy");
if (ModelState.IsValid)
{
using (var db = new MainDbContext())
{
Claim sessionEmail = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Email);
string userEmail = sessionEmail.Value;
var userIdQuery = db.Users.Where(u => u.Email == userEmail).Select(u => u.Id);
var userId = userIdQuery.ToList();
var dbAmsterdam = db.Amsterdam.Create();
dbAmsterdam.Details = list.Details;
dbAmsterdam.Date_Posted = dateToday;
dbAmsterdam.Time_Posted = timeToday;
dbAmsterdam.User_Id = userId[0];
db.Amsterdam.Add(dbAmsterdam);
db.SaveChanges();
}
}
else
{
ModelState.AddModelError("", "Incorrect format has been placed");
}
return View();
}
我知道我接近,因爲我註冊的工作,但我不能讓我的CRUD for Amsterdam
工作。當我點擊確定如截圖所示(當我點擊廣告項目):
https://i.stack.imgur.com/XsGws.png
我得到一個錯誤
DbUpdateException是由用戶代碼未處理
https://i.stack.imgur.com/I6kYP.png
它也說「看到內部異常」。你是否 ?它說什麼 ? – Shyju
{「無效的對象名'dbo.Amsterdams'。」} – DjRossoreTV
'db.Amsterdam.Create()'?這是做什麼的? – Shyju