私のプロジェクトのヘルプを探しています。入力フォームを使用してdbにオブジェクトを追加しようとしています。私はハードコードAddEventIdしようとフォーム内にそれを提供しています現時点ではASP.Net MVC 5難易度dbの追加オブジェクト
にHomeController
public class HomeController : Controller
{
EventsContext db = new EventsContext();
public ActionResult Index()
{
IEnumerable<Event> events = db.Events;
ViewBag.Events = events;
return View();
}
[HttpGet]
public ActionResult CreateEvent()
{
return View();
}
[HttpPost]
public ActionResult CreateEvent(AddEvent addEvent)
{
db.AddEvents.Add(addEvent);
db.SaveChanges();
return View("index");
}
}
、後で私はそれを変更します:ここに私のコードです。ここ は、入力フォームです:
<form method="post" action="">
<table>
<tr>
<td><p>Id :</p></td>
<td><input type="text" name="AddEventId" /> </td>
</tr>
<tr>
<td><p>Title :</p></td>
<td><input type="text" name="Title" /> </td>
</tr>
<tr>
<td><p>Date :</p></td>
<td><input type="text" name="Date" /> </td>
</tr>
<tr>
<td><p>Time :</p></td>
<td><input type="text" name="Time" /> </td>
</tr>
<tr>
<td><p>Address :</p></td>
<td><input type="text" name="Location" /></td>
<tr>
<td><p>Lecturer:</p></td>
<td><input type="text" name="Responsible" /> </td>
</tr>
<tr><td><input type="submit" value="Submit" /> </td><td></td></tr>
</table>
</form>
AddEventクラス:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PhClub.Models
{
public class AddEvent
{
public int AddEventId { get; set; }
public string Title { get; set; }
public string Date { get; set; }
public string Time { get; set; }
public string Location { get; set; }
public string Responsible { get; set; }
}
}
イベント・クラス:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PhClub.Models
{
public class Event
{
public int Id { get; set; }
public string Title { get; set; }
public string Date { get; set; }
public string Time { get; set; }
public string Location { get; set; }
public string Responsible { get; set; }
}
}
私は理解していればそれはハードコーディングされたイベント
public class EventDbInitializer : DropCreateDatabaseAlways<EventsContext>
{
protected override void Seed(EventsContext db)
{
db.Events.Add(new Event { Title = "Name of Event", Date = "18/11/2017", Time = "20:00", Location = "Adress", Responsible = "Name" });
db.Events.Add(new Event { Title = "Name of Event", Date = "03/03/2017", Time = "19:00", Location = "Adress", Responsible = "Name2" });
//db.Events.Add(new Event { Title = "Title3, Date = "04/03/2017", Time = "217:00", Location = "School", Responsible = "Name"});
base.Seed(db);
}
}
で[OK]を動作します正しくa私はsubmitを押して、CreateEvent postメソッドを使用し、Eventオブジェクトのすべての要素をdbのcreateおよびEventに渡す必要があります。その後、ページに値を入力する必要があります。しかし、新しい出来事はありません。私は何を見ていないのですか?フォームの投稿がある場合は?:
<form method="post" action="">
それによると
デビッドは、それを見ています –