在下面,按下提交输入按钮后,用户输入的数据应保存到名为“事件”的数据库中,数据模型为“事件”。点击此页面上的提交后,我刷新了存储在Azure
服务器上的“事件”表,但我没有看到任何更改。我已经很好地设置了连接字符串。
1 视图
<div class="form">
@using (Html.BeginForm())
{
@Html.ValidationSummary(false)
@Html.Label("Title");
@Html.TextBoxFor(m => m.Title, new { required = "required" });
<br /><br />
@Html.Label("Description");
@Html.TextBoxFor(m => m.Desc, new { placeholder = "Event details: optional" });
<br /><br />
<div class="Date">
@Html.Label("Start Date")
@Html.TextBoxFor(model => model.StartDate, new { id = "startDate", placeholder = "DD | MM | YYYY" })
<br /><br />
@Html.Label("Start Time")
@Html.TextBoxFor(model => model.StartDate, new { id = "starTime", placeholder = "00:00" })
</div>
<div class="Date">
@Html.Label("End Date")
@Html.TextBoxFor(model => model.EndDate, new { id = "endDate", placeholder = "DD | MM | YYYY" })
<br /><br />
@Html.Label("End Time")
@Html.TextBoxFor(model => model.EndTime, new { id = "endTime", placeholder = "00:00" })
</div>
<div class="clearedDiv">
@Html.Label("Attending")<br />
@Html.TextBoxFor(model => model.AttendingID, new { type = "number", placeholder = "00" })
</div>
<label class="checkType">@Html.CheckBoxFor(m => m.IsCatering, new { id = "catering" })<span></span></label>
<label for="catering">MIC will take care of catering for this event</label>
<div class="nextPage">
<input type="submit" value="Now choose your resources" />
</div>
}
</div>
2 模型
public class CreateEvent
{
[Required]
public String Title { get; set; }
public String Desc { get; set; }
//Converting a Markup field into type "Date"
[DataType(DataType.DateTime), Required(ErrorMessage="Event start date required")]
public DateTime StartDate { get; set; }
[DataType(DataType.DateTime), Required(ErrorMessage = "Event end date required")]
public DateTime EndDate { get; set; }
[DataType(DataType.DateTime), Required(ErrorMessage = "Event start time required")]
public DateTime StartTime { get; set; }
[DataType(DataType.DateTime), Required(ErrorMessage = "Event end time required")]
public DateTime EndTime { get; set; }
//Validating attendees input to integer.
[Required(ErrorMessage="Enter how many are attending")]
public int AttendingID { get; set; }
public bool IsCatering { get; set; }
}
3 控制器
[HttpPost]
public ActionResult Create_event(CreateEvent eventData)
{
if (ModelState.IsValid)
{
Event eventTable = new Event();
eventTable.Title = eventData.Title;
eventTable.Description = eventData.Desc;
eventTable.StartDate = eventData.StartDate;
eventTable.EndDate = eventData.EndDate;
eventTable.StartTime = eventData.StartTime;
eventTable.EndTime = eventData.EndTime;
eventTable.Attendees = eventData.AttendingID;
eventTable.CaterEvent = eventData.IsCatering;
db.Events.InsertOnSubmit(eventTable);
try
{
db.SubmitChanges();
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
return View("Create_event");
}
[HttpGet]
public ActionResult Create_event()
{
return View();
}
谢谢,对不起,如果它不在代码块中,我是新手:p