簡體   English   中英

如何更新MVC5 / C中的輔助表#

[英]How to Update a Secondary Table in MVC5 / C#

我在MVC 5中有一個基於我的事務表(ICS_Transactions)的編輯視圖。 這開箱即用,沒有任何問題。

我的表是規范化的,因此ICS_Transactions表僅保存實際供應的唯一ID。 編輯視圖更新了ICS_Transactions表,但我還需要對ICS_Supplies進行更新,以便在處理訂單時減少庫存。

MVC5和C#對我來說都是新手。 我來自舊的asp.net表單和VB.net,我只需編寫我的sql update語句並執行它。

我的第一個問題是。 我在哪里這樣做? 是否應該在編輯視圖的[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,在訂單處理后,將Element“OnHand”更新為現有的耗材量。 我想要做的是取“onhand”的值並更新ICS_Supplies表,如下所示

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

這是編輯視圖的HttpPost。

 // 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);
    }

在提供的教程和其他一些在線搜索的幫助下,我能夠解決問題如下:

基本上添加了少量代碼來查找ICS_Supplies表,其中Suppies_ID =在Transaction Table中的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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM