MVC控制器不会创建记录

时间:2012-09-14 19:00:45

标签: c# asp.net-mvc

当我尝试使用“创建”操作创建新记录时,它会失败,但没有错误,它就不存在。我不确定为什么不是或为什么没有错误。我正在使用“搜索”操作填充“创建”操作中的某些字段,然后填写其余信息。

[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>

}

1 个答案:

答案 0 :(得分:0)

很难说......你可以做的事情很少:   - 使用萤火虫来查看哪些参数被回发到您的行动中   - 围绕“创建”操作设置几个断点,以查看是否正在处理正确的对象/值。