C# MVC控制器不会创建记录
当我尝试用create操作创建一个新记录时,它失败了,但没有错误,只是不存在。我不太清楚为什么没有或者为什么没有错误。我使用搜索操作填充创建操作中的一些字段,然后填充其余信息C# MVC控制器不会创建记录,c#,asp.net-mvc,C#,Asp.net Mvc,当我尝试用create操作创建一个新记录时,它失败了,但没有错误,只是不存在。我不太清楚为什么没有或者为什么没有错误。我使用搜索操作填充创建操作中的一些字段,然后填充其余信息 [HttpPost] public ActionResult Create(Incident incident) { if (ModelState.IsValid) { db.Incidents.AddObject(incident);
[HttpPost]
public ActionResult Create(Incident incident)
{
if (ModelState.IsValid)
{
db.Incidents.AddObject(incident);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.IncidentCreatedBy = new SelectList(db.Creators, "CreatorID", "CreatorName", incident.IncidentCreatedBy);
ViewBag.ErrorType = new SelectList(db.Errors, "ErrorID", "ErrorDescription", incident.ErrorType);
ViewBag.IncidentID = new SelectList(db.IncidentItems, "IncidentID", "IncidentItem1", incident.IncidentID);
ViewBag.IncidentReasonID = new SelectList(db.IncidentReasons, "IncidentReasonID", "ReasonDescription", incident.IncidentReasonID);
ViewBag.IncidentResolutionID = new SelectList(db.IncidentResolutions, "IncidentResolutionID", "ResolutionDescription", incident.IncidentResolutionID);
ViewBag.InventoryStatus = new SelectList(db.InventoryStatus, "InvStatusID", "InvStatusValue", incident.InventoryStatus);
ViewBag.ShipperID = new SelectList(db.ShipperIDs, "ShipperID1", "ShipperName", incident.ShipperID);
return View(incident);
}
public ActionResult Search(string searchString)
{
Incident i = new Incident();
if (!String.IsNullOrEmpty(searchString))
{
Tracking result = db1.Trackings.FirstOrDefault(r => r.OrderNum == searchString);
if (result == null)
{
return View();
}
else
{
i.Email = result.Email;
i.OriginalShipDate = result.ShipDate;
i.Name = result.Name;
i.OrderID = result.OrderNum;
i.IncidentReasonID = 1;
ViewBag.IncidentCreatedBy = new SelectList(db.Creators, "CreatorID", "CreatorName", i.IncidentCreatedBy);
ViewBag.ErrorType = new SelectList(db.Errors, "ErrorID", "ErrorDescription", i.ErrorType);
ViewBag.IncidentID = new SelectList(db.IncidentItems, "IncidentID", "IncidentItem1", i.IncidentID);
ViewBag.IncidentReasonID = new SelectList(db.IncidentReasons, "IncidentReasonID", "ReasonDescription", i.IncidentReasonID);
ViewBag.IncidentResolutionID = new SelectList(db.IncidentResolutions, "IncidentResolutionID", "ResolutionDescription", i.IncidentResolutionID);
ViewBag.InventoryStatus = new SelectList(db.InventoryStatus, "InvStatusID", "InvStatusValue", i.InventoryStatus);
ViewBag.ShipperID = new SelectList(db.ShipperIDs, "ShipperID1", "ShipperName", i.ShipperID);
return PartialView("~/Views/Home/Create.cshtml", i);
}
}
else { return View(); }
}
//search view
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@using (Html.BeginForm("Search", "Home", FormMethod.Post)){
<p> Order #: @Html.TextBox("SearchString") <br />
<input type="submit" value="Filter" /></p>
}
@if(IsPost){}
--create view
@model RMA_MAID_EASY.Models.Incident
@{
ViewBag.Title = "Create";
}
<script type="text/javascript" ></script>
<script>
$(function () {
$("#datepicker").datepicker();
});
</script>
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Incidents</legend>
<table>
@*<tbody>*@
<tr>
<td><div class="editor-label">@Html.LabelFor(model => model.OrderID)</div> <div class="editor-field">@Html.EditorFor(model => model.OrderID)@Html.ValidationMessageFor(model => model.OrderID)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.Email)</div><div class="editor-field">@Html.EditorFor(model => model.Email)@Html.ValidationMessageFor(model => model.Email)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.Name)</div><div class="editor-field">@Html.EditorFor(model => model.Name)@Html.ValidationMessageFor(model => model.Name)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.OriginalShipDate)</div><div class="editor-field">@Html.EditorFor(model => model.OriginalShipDate)@Html.ValidationMessageFor(model => model.OriginalShipDate)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.IncidentReasonID, "IncidentReason")</div><div class="editor-field">@Html.DropDownList("IncidentReasonID", String.Empty)@Html.ValidationMessageFor(model => model.IncidentReasonID)</div></td>
</tr>
<tr>
<td><div class="editor-label">@Html.LabelFor(model => model.IncidentResolutionID, "IncidentResolution")</div><div class="editor-field">@Html.DropDownList("IncidentResolutionID", String.Empty)@Html.ValidationMessageFor(model => model.IncidentResolutionID)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.Notes)</div><div class="editor-field">@Html.EditorFor(model => model.Notes)@Html.ValidationMessageFor(model => model.Notes)</div></td>
@*<td><div class="editor-label">@Html.LabelFor(model => model.ReturnTagRequired)</div><div class="editor-field">@Html.EditorFor(model => model.ReturnTagRequired)@Html.ValidationMessageFor(model => model.ReturnTagRequired)</div></td>*@
<td><div class="editor-label">@Html.LabelFor(model => model.ReturnTagRequired)</div><div class="editor-field">@Html.DropDownListFor(x => x.RefundRequired, new SelectList(new Dictionary<string, string> { {"Y", "Yes"}, {"N", "No"} },"key", "value", Model.RefundRequired))</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.DateRetTagGenerated)</div><div class="editor-field">@Html.TextBoxFor(model => model.DateRetTagGenerated, new { id = "datepicker", type = "text" })@Html.ValidationMessageFor(model => model.DateRetTagGenerated)</div></td>
@*<td><div class="editor-label">@Html.LabelFor(model => model.RefundRequired)</div><div class="editor-field">@Html.EditorFor(model => model.RefundRequired)@Html.ValidationMessageFor(model => model.RefundRequired)</div></td>*@
<td><div class="editor-label">@Html.LabelFor(model => model.RefundRequired)</div><div class="editor-field">@Html.DropDownListFor(x => x.RefundRequired, new SelectList(new Dictionary<string, string> { {"Y", "Yes"}, {"N", "No"} },"key", "value", Model.RefundRequired)) </div></td>
</tr>
<tr bgcolor ="#D3D3D3">
<td><div class="editor-label">@Html.LabelFor(model => model.RefundAmount)</div><div class="editor-field">@Html.EditorFor(model => model.RefundAmount)@Html.ValidationMessageFor(model => model.RefundAmount)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.DateRefundIssued)</div><div class="editor-field">@Html.EditorFor(model => model.DateRefundIssued)@Html.ValidationMessageFor(model => model.DateRefundIssued)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.ShipperID, "ShipperID1")</div><div class="editor-field">@Html.DropDownList("ShipperID", String.Empty)@Html.ValidationMessageFor(model => model.ShipperID)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.InventoryItem)</div><div class="editor-field">@Html.EditorFor(model => model.InventoryItem)@Html.ValidationMessageFor(model => model.InventoryItem)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.InventoryQuantity)</div><div class="editor-field">@Html.EditorFor(model => model.InventoryQuantity)@Html.ValidationMessageFor(model => model.InventoryQuantity)</div></td>
</tr>
<tr>
<td><div class="editor-label">@Html.LabelFor(model => model.eBayCredit)</div><div class="editor-field">@Html.EditorFor(model => model.eBayCredit)@Html.ValidationMessageFor(model => model.eBayCredit)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.eBayCreditFileDate)</div><div class="editor-field">@Html.EditorFor(model => model.eBayCreditFileDate)@Html.ValidationMessageFor(model => model.eBayCreditFileDate)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.LostValue)</div><div class="editor-field">@Html.EditorFor(model => model.LostValue)@Html.ValidationMessageFor(model => model.LostValue)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.IncidentCreatedBy, "Creator")</div><div class="editor-field">@Html.DropDownList("IncidentCreatedBy", String.Empty)@Html.ValidationMessageFor(model => model.IncidentCreatedBy)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.IncidentReturnDate)</div><div class="editor-field">@Html.EditorFor(model => model.IncidentReturnDate)@Html.ValidationMessageFor(model => model.IncidentReturnDate)</div></td>
</tr>
<tr bgcolor ="#D3D3D3">
<td><div class="editor-label">@Html.LabelFor(model => model.IncidentResolved)</div><div class="editor-field">@Html.EditorFor(model => model.IncidentResolved)@Html.ValidationMessageFor(model => model.IncidentResolved)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.IncidentReceived)</div><div class="editor-field">@Html.EditorFor(model => model.IncidentReceived)@Html.ValidationMessageFor(model => model.IncidentReceived)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.ShippingCost)</div><div class="editor-field">@Html.EditorFor(model => model.ShippingCost)@Html.ValidationMessageFor(model => model.ShippingCost)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.IncidentCreatedDate)</div><div class="editor-field">@Html.EditorFor(model => model.IncidentCreatedDate)@Html.ValidationMessageFor(model => model.IncidentCreatedDate)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.RMANumber)</div><div class="editor-field">@Html.EditorFor(model => model.RMANumber)@Html.ValidationMessageFor(model => model.RMANumber)</div></td>
</tr>
<tr>
<td><div class="editor-label">@Html.LabelFor(model => model.RMARequested)</div><div class="editor-field">@Html.EditorFor(model => model.RMARequested)@Html.ValidationMessageFor(model => model.RMARequested)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.RestockOption)</div><div class="editor-field">@Html.EditorFor(model => model.RestockOption)@Html.ValidationMessageFor(model => model.RestockOption)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.ReceivedNotes)</div><div class="editor-field">@Html.EditorFor(model => model.ReceivedNotes)@Html.ValidationMessageFor(model => model.ReceivedNotes)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.InventoryStatus, "InventoryStatu")</div><div class="editor-field">@Html.DropDownList("InventoryStatus", String.Empty)@Html.ValidationMessageFor(model => model.InventoryStatus)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.ErrorType, "Error")</div><div class="editor-field">@Html.DropDownList("ErrorType", String.Empty)@Html.ValidationMessageFor(model => model.ErrorType)</div></td>
</tr>
<tr bgcolor ="#D3D3D3">
<td><div class="editor-label">@Html.LabelFor(model => model.ReplacementOption)</div><div class="editor-field">@Html.EditorFor(model => model.ReplacementOption)@Html.ValidationMessageFor(model => model.ReplacementOption)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.Replacement)</div><div class="editor-field">@Html.EditorFor(model => model.Replacement)@Html.ValidationMessageFor(model => model.Replacement)</div></td>
<td><div class="editor-label">@Html.LabelFor(model => model.OurShippingCost)</div><div class="editor-field">@Html.EditorFor(model => model.OurShippingCost)@Html.ValidationMessageFor(model => model.OurShippingCost)</div>
</tr>
@*</tbody>
*@</table>
<p>
<input type="submit" value="Create" />
@Html.ActionLink("Back to List", "Index")
</p>
</fieldset>
[HttpPost]
公共行动结果创建(事件)
{
if(ModelState.IsValid)
{
db.Incidents.AddObject(事件);
db.SaveChanges();
返回操作(“索引”);
}
ViewBag.IncidentCreatedBy=新建选择列表(db.Creators,“CreatorID”,“CreatorName”,incident.IncidentCreatedBy);
ViewBag.ErrorType=新选择列表(db.Errors,“ErrorID”,“ErrorDescription”,incident.ErrorType);
ViewBag.IncidentID=新建选择列表(db.IncidentItems,“IncidentID”,“IncidentItem1”,IncidentID.IncidentID);
ViewBag.IncidentReasonID=新选择列表(db.IncidentReasonID,“IncidentReasonID”,“ReasonDescription”,incident.IncidentReasonID);
ViewBag.IncidentResolutionID=新选择列表(db.IncidentResolutions,“IncidentResolutionID”,“ResolutionDescription”,incident.IncidentResolutionID);
ViewBag.InventoryStatus=新的选择列表(db.InventoryStatus,“InvStatusID”,“InvStatusValue”,incident.InventoryStatus);
ViewBag.ShipperID=新的选择列表(db.ShipperIDs,“shipperd1”,“ShipperName”,incident.ShipperID);
返回视图(事件);
}
公共操作结果搜索(字符串搜索字符串)
{
事件i=新事件();
如果(!String.IsNullOrEmpty(searchString))
{
跟踪结果=db1.Trackings.FirstOrDefault(r=>r.OrderNum==searchString);
如果(结果==null)
{
返回视图();
}
其他的
{
i、 Email=result.Email;
i、 OriginalShipDate=结果.ShipDate;
i、 Name=result.Name;
i、 OrderID=result.OrderNum;
i、 IncidentReasonID=1;
ViewBag.IncidentCreatedBy=新建选择列表(db.Creators,“CreatorID”,“CreatorName”,i.IncidentCreatedBy);
ViewBag.ErrorType=新选择列表(db.Errors,“ErrorID”,“ErrorDescription”,i.ErrorType);
ViewBag.IncidentID=新建选择列表(db.IncidentItems,“IncidentID”,“IncidentItem1”,i.IncidentID);
ViewBag.IncidentReasonID=新选择列表(db.IncidentReasonID,“IncidentReasonID”,“ReasonDescription”,i.IncidentReasonID);
ViewBag.IncidentResolutionID=新选择列表(db.IncidentResolutions,“IncidentResolutionID”,“ResolutionDescription”,i.IncidentResolutionID);
ViewBag.InventoryStatus=新建选择列表(db.InventoryStatus,“InvStatusID”,“InvStatusValue”,i.InventoryStatus);
ViewBag.ShipperID=新的选择列表(db.ShipperIDs,“shipperd1”,“ShipperName”,i.ShipperID);
返回PartialView(“~/Views/Home/Create.cshtml”,i);
}
}
else{return View();}
}
//搜索视图
@ActionLink(“返回列表”、“索引”)
@使用(Html.BeginForm(“Search”,“Home”,FormMethod.Post)){
订单:@Html.TextBox(“搜索字符串”)
}
@if(IsPost){}
--创建视图
@模型RMA_MAID_EASY.Models.Incident
@{
ViewBag.Title=“创建”;
}
$(函数(){
$(“#日期选择器”).datepicker();
});
创造
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
事件
@**@
@LabelFor(model=>model.OrderID)@Html.EditorFor(model=>model.OrderID)@Html.ValidationMessageFor(model=>model.OrderID)
@LabelFor(model=>model.Email)@Html.EditorFor(model=>model.Email)@Html.ValidationMessageFor(model=>model.Email)
@LabelFor(model=>model.Name)@Html.EditorFor(model=>model.Name)@Html.ValidationMessageFor(model=>model.Name)
@Html.LabelFor(model=>model.OriginalShipDate)@Html.EditorFor(model=>model.OriginalShipDate)@Html.ValidationMessageFor(model=>model.OriginalShipDate)
@LabelFor(model=>model.IncidentReasonID,“IncidentReasonID”)@Html.DropDownList(“IncidentReasonID”,String.Empty)@Html.ValidationMessageFor(model=>model.IncidentReasonID)
@Html.LabelFor(model=>model.IncidentResolutionID,“IncidentResolutionID”)@Html.DropDownList(“IncidentResolutionID”,String.Empty)@Html.ValidationMessageFor(model=>model.IncidentResolutionID)
@LabelFor(model=>model.Notes)@Html.EditorFor(model=>model.Notes)@Html.ValidationMessageFor(model=>model.Notes)
@*@Html.LabelFor(model=>model.ReturnTagRequired)@Html.EditorFor(model=>model.ReturnTagRequired)@Html.ValidationMessageFor(model=>model.ReturnTagRequired)*@
@Html.LabelFor(model=>model.ReturnTagRequired)@Html.DropDownListFor(x=>x.returnrequired,新选择列表(新字典{{“Y”,“Yes”},{“N”,“No”},“key”,“value”,model.returnrequired))
@Html.LabelFor(model=>model.DateRetTagGenerated)@Html.TextBoxFor(model=>model.DateRetTagGenerated,new{id=“datepicker”,type=“text”})@Html.ValidationMessageFor(model=>model.DateRetTagGenerated)
@*@LabelFor(model=>model.returnRequired)@Html.EditorFor(model=>model.returnRequired)@Html.ValidationMessageFor(model=>model.returnRequired)*@
@Html.LabelFor(model=>model.returnRequired)@Html.DropDownListFor(x=>x.returnRequired,新选择列表(新字典{{{“Y”,“Yes”},{“N”,“No”},“key”,“value”,model.returnRequired))
@LabelFor(model=>model.returnamount)@Html.EditorFor(model=>model.returnamount)@Html.ValidationMessageFor(model=>model.returnamount)
@Html.LabelFor(model=>model.datereturnissued)@Html.EditorFor(model=>model.datereturnissued)@Html.ValidationMessageFor(model=>model.datereturnissued)
@LabelFor(mod