javascript – 提交两个表格是可能的吗? asp.net mvc

我在一个页面中有两个表单,两个表单分别有一个保存按钮.

每当我点击另一个按钮时,我都希望保存我在其他表单上添加的更改.

这是我的代码:

<div id="contentMain">


         @using (Html.BeginForm("ClientLocationSave", "Client", FormMethod.Post, new { id = "clientLocForm" }))
            {
            <input type="hidden" id="clientId" name="clientId" value="@ViewBag.ClientId" />
            <input type="hidden" id="clientLocId" name="clientLocId" value="@clientLocId" /> 

        <h2>
            Client Location @pageAction</h2>
        <div class="main">
            <p>
                <label for="txtName">
                    Name</label>
                <span>
                    <input type="text" id="txtName" name="txtName" class="validate[required] inputLong" value="@clientLocName" />
                </span>
            </p>
            <p>
                <label for="txtAddress1">
                    Address 1</label>
                <span>
                    <input type="text" id="txtAddress1" name="txtAddress1" class="validate[required] inputLong" value="@addressLine1" />
                </span>
            </p>
            <p>
                <label for="txtAddress2">
                    Address 2</label>
                <span>
                    <input type="text" id="txtAddress2" name="txtAddress2" class="inputLong" value="@addressLine2" />
                </span>
            </p>
            <p>
                <label for="txtCity">
                    City</label>
                <span>
                    <input type="text" id="txtCity" name="txtCity" class="validate[required] inputLong" value="@city" />
                </span>
            </p>
            <p>
                <label for="ddlState">
                    State</label>
                <span>
                    @Html.DropDownList("ddlState", new SelectList(ViewBag.StateList, "ID", "Display_Value", state), "[Please Select]",
                new Dictionary<string, object>
                {
                    {"class","validate[required] inputLong"}
                })
                </span>
            </p>
            <p>
                <label for="txtZipCode">
                    Zip Code</label>
                <span>
                    <input type="text" id="txtZipCode" name="txtZipCode" class="validate[required,custom[onlyNumberSp],maxSize[20]] inputLong" value="@zipCode" />
                </span>
            </p>
        </div>
        <input type="submit" id="btnSave" class="styledButton" value="Save" />

    }
 <div class="main">
        @using (Html.BeginForm("ClientLocationContactSave", "Client", FormMethod.Post, new { id = "contactForm" }))
        {
            <input type="hidden" id="clientId" name="clientId" value="@clientId" />
            <input type="hidden" id="clientLoctContactId" name="clientLoctContactId" value="@clientLoctContactId" />
            <input type="hidden" id="clienLocatId" name="clienLocatId" value="@clientLocId" />

            <p>
                <label for="ddlContact">
                    Contact Type</label>
                <span>
                    @Html.DropDownList("ddlContact", new SelectList(ViewBag.ContactType, "ID", "Display_Value", contactTypeLookId), "[Please Select]",
                new Dictionary<string, object>
                {
                    {"class","validate[required] inputLong"}
                })
                </span>
            </p>
            <p>
                <label for="txtValue">
                    Contact Value</label>
                <span>
                    <input type="text" id="txtValue" name="txtValue" class="validate[required] inputLong"
                        value="" />
                    <p>
                        <label for="chkSaveIsPrimary">
                            Is Primary</label>
                        <input type="checkbox" name="chkSaveIsPrimary" id="chkSaveIsPrimary" value="true" checked="checked" />
                    </p>
                </span>
            </p> 
            <script type="text/javascript">
                $(document).ready(function () {
                    var disableFields = $('#clienLocatId').val();
                    if (disableFields == 0) {
                        $('#disable').attr("hidden", false);
                        $('#txtValue').attr("disabled", true);
                        $('#ddlContact').attr("disabled", true);
                        $('#chkSaveIsPrimary').attr("disabled", true);

                    }
                    else {
                        $('#disable').attr("hidden", true);
                        $('#txtValue').attr("disabled", false);
                        $('#ddlContact').attr("disabled", false);
                        $('#chkSaveIsPrimary').attr("disabled", false);

                    }
                });


            </script>



            <p>
                <span>
                    <input type="submit" id="btnAddLocationContact" name="btnAddLocationContact" class="styledButton"
                        value="Add Contact" />
                </span>
            </p>
        }
    </div>
CONTROLLER:
public ActionResult ClientLocationSave(FormCollection formCollection)
        {
            String msg = String.Empty;
            String newClientLocationId = String.Empty;
            String clientId = formCollection["clientId"];
            String clientLocId = formCollection["clientLocId"];
            String locationName = formCollection["txtName"];
            String address1 = formCollection["txtAddress1"];
            String address2 = formCollection["txtAddress2"];
            String city = formCollection["txtCity"];
            String state = formCollection["ddlState"];
            String zipCode = formCollection["txtZipCode"];

            Client_Location clientLoc = new Client_Location();
            try
            {
                if (String.IsNullOrWhiteSpace(clientLocId) || clientLocId == "0")
                {
                    clientLoc.ClientID = Convert.ToInt32(clientId);
                    clientLoc.Name = locationName.Trim();
                    clientLoc.Address_Line1 = address1;
                    clientLoc.Address_Line2 = address2;
                    clientLoc.City = city;
                    clientLoc.State_LookID = Convert.ToInt32(state);
                    clientLoc.ZipCode = zipCode;
                    clientLoc.DateCreated = DateTime.UtcNow;
                    clientLoc.DateModified = DateTime.UtcNow;
                    clientLoc.CreatedBy = User.Identity.Name;
                    clientLoc.ModifiedBy = User.Identity.Name;

                    db.Client_Location.Add(clientLoc);
                }
                else
                {
                    int id = Convert.ToInt32(clientLocId);
                    clientLoc = (from a in db.Client_Location
                                 where a.ID == id
                                 select a).SingleOrDefault();

                    clientLoc.Name = locationName.Trim();
                    clientLoc.Address_Line1 = address1;
                    clientLoc.Address_Line2 = address2;
                    clientLoc.City = city;
                    clientLoc.State_LookID = Convert.ToInt32(state);
                    clientLoc.ZipCode = zipCode;
                    clientLoc.DateModified = DateTime.UtcNow;
                    clientLoc.ModifiedBy = User.Identity.Name;
                }
            }
            catch (Exception)
            {
                msg = "Failed to save";
            }



            db.SaveChanges();
            if (String.IsNullOrWhiteSpace((msg)))
            { TempData["message"] = "Client Location Saved Successfully."; }
            else if (msg != "")
            { TempData["message"] = msg; }

            newClientLocationId = clientLoc.ID.ToString();

            return RedirectToAction("ClientLocationDetails", new { clientId = clientId, clientLocId = newClientLocationId });

        }
  public ActionResult ClientLocationContactSave(FormCollection formCollection)
        {
            String msg = String.Empty;
            String clientId = formCollection["clientId"];
            String clientLoctContactId = formCollection["clientLoctContactId"];
            String clienLocatId = formCollection["clienLocatId"];
            bool isPrimary = Convert.ToBoolean(formCollection["chkSaveIsPrimary"]);
            String value = formCollection["txtValue"];
            String contactTypeLookId = formCollection["ddlContact"];


            Client_Location_Contact clientLoc = new Client_Location_Contact();
            try
            {
                if (String.IsNullOrWhiteSpace(clientLoctContactId) || clientLoctContactId == "0")
                {
                    clientLoc.Client_LocationID = Convert.ToInt32(clienLocatId);
                    clientLoc.Value = value.Trim();
                    clientLoc.IsPrimary = isPrimary;
                    clientLoc.ContactType_LookID = Convert.ToInt32(contactTypeLookId);
                    clientLoc.DateCreated = DateTime.UtcNow;
                    clientLoc.DateModified = DateTime.UtcNow;
                    clientLoc.CreatedBy = User.Identity.Name;
                    clientLoc.ModifiedBy = User.Identity.Name;

                    db.Client_Location_Contact.Add(clientLoc);
                }
                else
                {
                    int id = Convert.ToInt32(clientLoctContactId);
                    clientLoc = (from a in db.Client_Location_Contact
                                 where a.ID == id
                                 select a).SingleOrDefault();

                    clientLoc.Value = value.Trim();
                    clientLoc.IsPrimary = isPrimary;
                    clientLoc.ContactType_LookID = Convert.ToInt32(contactTypeLookId);
                    clientLoc.DateModified = DateTime.UtcNow;
                    clientLoc.ModifiedBy = User.Identity.Name;
                }
            }
            catch (Exception)
            {
                msg = "Failed to save";
            }


            db.SaveChanges();
            if (String.IsNullOrWhiteSpace((msg)))
            { TempData["message"] = "Contact Saved Successfully."; }
            else if (msg != "")
            { TempData["message"] = msg; }


            ViewBag.clientLoctContactId = clientLoctContactId;
            ViewBag.clienLocatId = clienLocatId;
            return RedirectToAction("ClientLocationDetails", new { clientLocId = clienLocatId, clientId = clientId });
        }

这可以用jQuery完成,如果是 – 如何?

最佳答案 让我们把你的问题重新改成一些更抽象的东西来帮助你:

您有两份纸质表格需要签名并提供给您的经理.一个人必须在人力资源部门给弗雷德,另一个人在建筑物的另一边给予销售中的威尔玛.

你可以离开办公桌,一次签署吗?当然不是.您需要选择一个先做,然后转到第二个,最后到达您的经理,并签署两个表格.

对于您的页面也是如此,您可以将它们全部捆绑到一个表单中并将其提交给服务器,处理第一部分,然后获取一些其他代码来处理第二部分,然后将结果返回给用户.

虽然你可以用一些花哨的技巧来解决这个问题,但你需要问问自己为什么要这么做.如果你总是保存两个表格,那么为什么要打扰两个?

点赞