如何在MVC5 / C#中更新辅助表

时间:2019-05-20 12:54:23

标签: c# asp.net-mvc-5

我在MVC 5中有一个基于我的事务表(ICS_Transactions)的编辑视图。开箱即用,没有问题。

我的表已经标准化,因此ICS_Transactions表仅包含实际供应的唯一ID。编辑视图更新了ICS_Transactions表,但是在处理订单时,我还需要对ICS_Supplies进行更新以减少现有库存。

MVC5和C#对我来说很新。我来自旧的asp.net表单和VB.net,在这里我只需编写我的sql更新语句并执行它。

我的第一个问题是。 。我在哪里做?它应该在[HttpPost]控制器操作中为“编辑”视图执行吗?或为Jscript / jQuery添加onclick事件(这对我来说也是很新的,我对如何执行此操作并不了解,但是可以在线搜索)。我的初步研究表明,这可以在Ajax中完成吗?但是。 。 。我的部门不是Ajax的支持者,如果可能的话,希望保持联系。不知道为什么。

这是我的编辑控制器的代码

@model ICS20web.Models.ICS_Transactions

<style>
.pos {
    color: black;
    font-weight: normal;
}

.neg {
    color: red;
    font-weight: bold;
}
</style>



@{
ViewBag.Title = "Edit";
}


@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>ICS:  Process Order</h4>




    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.TransID)

    @ViewBag.MyUnit &nbsp; &nbsp; @ViewBag.MyBuilding &nbsp; &nbsp;   @ViewBag.MyStreet &nbsp; &nbsp; @ViewBag.MyCity &nbsp; &nbsp; @ViewBag.MyState &nbsp; &nbsp; @ViewBag.MyZip &nbsp; &nbsp; @ViewBag.MyContact &nbsp; &nbsp; @ViewBag.MyPhone

    <hr />

    <h5><b>Order Details:  @ViewBag.Description</b></h5>

    <!-- Begin Drop Down For Current Order Status-->
    <div class="form-group">
        @Html.Label("Update Current Order Progress:", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="form-control">
                <select name="CheckStatus" id="CheckStatus">

                    <option value=@ViewBag.MyStatus>@ViewBag.MyStatus</option>
                    <option value="Cancelled">Cancelled</option>
                    <option value="Processing">Processing</option>
                    <option value="On Hold">On Hold</option>
                    <option value="Complete">Complete</option>
                    <option value="Low Stock Backordered">Low Stock Backordered</option>
                    <option value="Order Processed">Order Placed</option>

                </select>
            </div>
        </div>
    </div>

    <hr />

    <!-- End Drop Down For Order Current Status-->

    <!-- Begin Drop Down For Open/Closed-->
    <div class="form-group">
        @Html.Label("Close Order:", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="form-control">
                <select name="CheckOpen" id="CheckOpen">

                    <option value="Open">Open</option>
                    <option value="Closed">Closed</option>


                </select>
            </div>
        </div>
    </div>

    <hr />
    <!-- End Drop Down For Open/Closed-->


    <!-- Begin Comments Area-->
    <div class="form-group">
        @Html.LabelFor(model => model.Comments, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextAreaFor(model => model.Comments, 5, 100, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Comments, "", new { @class = "text-danger" })
        </div>
    </div>
    <!-- End Comments Area-->




    <div class="form-group">
        @Html.LabelFor(model => model.UnitsOrdered, (string)"Units On Hand ", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">              
            @Html.TextBox("OnHand", null, new { @class = "form-control", Value = ViewBag.Stock, @readonly = "readonly" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.UnitsOrdered, (string)"Number of Units Ordered", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UnitsOrdered, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
            @ViewBag.MyCountIntro @ViewBag.MyCount
            @Html.ValidationMessageFor(model => model.UnitsOrdered, "", new { @class = "text-danger" })

        </div>
    </div>


    <div class="form-group">
        @Html.LabelFor(model => model.UnitsOrdered, (string)"Units Remaining ", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBox("Remaining", null, new { @class = "form-control", Value = ViewBag.Total, @readonly = "readonly" })
        </div>
    </div>




            <div class="form-group">
                @Html.LabelFor(model => model.OriginalDate, (string)"Original Order Date", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.OriginalDate, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                    @Html.ValidationMessageFor(model => model.OriginalDate, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.TransType, (string)"Transaction Type", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.TransType, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                    @Html.ValidationMessageFor(model => model.TransType, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.LastUpdatedBy, (string)"Last Updated By", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.LastUpdatedBy, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", Value = ViewBag.ThisUser } })
                    @Html.ValidationMessageFor(model => model.LastUpdatedBy, "", new { @class = "text-danger" })
                </div>
            </div>



            <div class="form-group">
                @Html.LabelFor(model => model.OpenClosed, (string)"Open / Closed", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.OpenClosed, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                    @Html.ValidationMessageFor(model => model.OpenClosed, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.CurrentStatus, (string)"Current Status", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.CurrentStatus, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                    @Html.ValidationMessageFor(model => model.CurrentStatus, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.CurrentStatusDate, (string)"Current Status Date", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.CurrentStatusDate, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                    @Html.ValidationMessageFor(model => model.CurrentStatusDate, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.RequsitionNumber, (string)"Requisition Number", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.RequsitionNumber, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                    @Html.ValidationMessageFor(model => model.RequsitionNumber, "", new { @class = "text-danger" })
                </div>
            </div>


            <div class="form-group">
                @Html.LabelFor(model => model.DeliveryMonth, (string)"Expected Delivery Month", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.DeliveryMonth, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                    @Html.ValidationMessageFor(model => model.DeliveryMonth, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.DeliveryYear, (string)"Expected Delivery Year", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.DeliveryYear, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                    @Html.ValidationMessageFor(model => model.DeliveryYear, "", new { @class = "text-danger" })
                </div>
            </div>


            <div class="form-group">
                @Html.LabelFor(model => model.Emergency, (string)"Order Is Emergency", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Emergency, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                    @Html.ValidationMessageFor(model => model.Emergency, "", new { @class = "text-danger" })
                </div>
            </div>


            <div class="form-group" >
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Save" class="btn btn-default" onclick="myFunction();" />
                </div>
            </div>

    <!-- Begin Hidden Fields-->
    <div class="form-group">

        <div class="col-md-10">
            @Html.HiddenFor(model => model.SuppliesID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
            @Html.ValidationMessageFor(model => model.SuppliesID, "", new { @class = "text-danger" })
        </div>
    </div>

      <div class="form-group">

        <div class="col-md-10">
            @Html.Hidden("OrigStock", null, new { @class = "form-control", Value = ViewBag.Stock, @hidden = "hidden" })
        </div>
    </div>

    <div class="form-group">

        <div class="col-md-10">
            @Html.HiddenFor(model => model.Contact, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
            @Html.ValidationMessageFor(model => model.Contact, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">

        <div class="col-md-10">
            @Html.HiddenFor(model => model.DeliveryUnitID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
            @Html.ValidationMessageFor(model => model.DeliveryUnitID, "", new { @class = "text-danger" })
        </div>
    </div>





    <!-- End Hidden Fields-->
        </div>
        }

        <div>
            @Html.ActionLink("Back to List", "Index")
        </div>


        <!-- Add Java Script-->

        @Scripts.Render("~/Scripts/jquery-1.10.2.min.js")
        @Scripts.Render("~/Scripts/CheckContacts.js")
        @Scripts.Render("~/Scripts/Supplies.js")
        @Scripts.Render("~/Scripts/ProcessOrder.js")

我添加了一些Jscript,可在处理订单后将元素“ OnHand”更新为手头上的供应量。而我想做的就是获取“ onhand”的值,并如下更新ICS_Supplies表

更新ICS_Supplies 设置OnHand = 元素OnHand的值 其中Supplies_ID = 元素SuppliesID的值

这是我的HttpPost的Edit视图。 。 。

 // POST: OrderManagement/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "TransID,SuppliesID,OriginalDate,TransType,LastUpdatedBy,Contact,OpenClosed,CurrentStatus,CurrentStatusDate,RequsitionNumber,PONumber,DeliveryMonth,DeliveryYear,UnitsOrdered,Emergency,Comments,DeliveryUnitID")] ICS_Transactions iCS_Transactions)
    {


        if (ModelState.IsValid)
        {
            db.Entry(iCS_Transactions).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(iCS_Transactions);
    }

1 个答案:

答案 0 :(得分:0)

借助所提供的教程的帮助以及其他一些在线搜索,我能够按以下方法解决此问题:

基本上添加了少量代码来查找ICS_Supplies表,其中Suppies_ID =事务表中的SuppliesID并更新

谢谢您的帮助!

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "TransID,SuppliesID,OriginalDate,TransType,LastUpdatedBy,Contact,OpenClosed,CurrentStatus,CurrentStatusDate,RequsitionNumber,PONumber,DeliveryMonth,DeliveryYear,UnitsOrdered,Emergency,Comments,DeliveryUnitID")] ICS_Transactions iCS_Transactions)
    {
        if (ModelState.IsValid)
        {
            //lookup ICS_Supplies
            var supplyEntity = db.ICS_Supplies.Where(s => s.Supplies_ID == iCS_Transactions.SuppliesID).FirstOrDefault();
            if (supplyEntity != null)
            {
                supplyEntity.OnHand = supplyEntity.OnHand - iCS_Transactions.UnitsOrdered;
            }
            db.Entry(iCS_Transactions).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(iCS_Transactions);
    }