MVC控制器将不会创建记录 [英] MVC Controller Will Not Create Record

查看:81
本文介绍了MVC控制器将不会创建记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用Create操作创建新记录时,它失败了,但是没有错误,只是没有出现.我不确定为什么不这样做或为什么没有错误.我正在使用搜索"操作填充创建"操作中的某些字段,然后填写其余信息.

When I try to create a new record with the Create action it fails but there is no errors it's just not there. I'm not exactly sure why its not or why there isn't an error. I'm using a Search action to populate some of the fields in the Create action then filling in the rest of the information.

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

}

推荐答案

很难说……您可以做的几件事:-使用萤火虫查看将哪些参数发布回您的操作-在创建"操作周围设置一些断点,以查看是否处理了正确的对象/值.

Its hard to say ... few things you could do: - Use firebug to see what params are being posted back to your action - Set a few breakpoints around your Create action to see if the correct objects/values are being dealt with.

这篇关于MVC控制器将不会创建记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆