Javascript 可以提交两份表格吗?asp.NETMVC

Javascript 可以提交两份表格吗?asp.NETMVC,javascript,jquery,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-2,Javascript,Jquery,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 2,我在一个页面中有两个表单,并且都分别有一个保存按钮。 无论何时单击“其他”按钮,我都希望在其他窗体上添加的更改也能保存 这是我的代码: <div id="contentMain"> @using (Html.BeginForm("ClientLocationSave", "Client", FormMethod.Post, new { id = "clientLocForm" })) { <input typ

我在一个页面中有两个表单,并且都分别有一个保存按钮。 无论何时单击“其他”按钮,我都希望在其他窗体上添加的更改也能保存

这是我的代码:

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

@使用(Html.BeginForm(“ClientLocationSave”,“Client”,FormMethod.Post,new{id=“clientLocForm”}))
{
客户端位置@pageAction

名称

地址1

地址2

城市

陈述 @Html.DropDownList(“ddlState”,新选择列表(ViewBag.StateList,“ID”,“Display_Value”,state),“[请选择]”, 新词典 { {“类”,“验证[必需]输入长度”} })

邮政编码

} @使用(Html.BeginForm(“ClientLocationContactSave”,“Client”,FormMethod.Post,new{id=“contactForm”})) { 接触式 @Html.DropDownList(“ddlContact”,新选择列表(ViewBag.ContactType,“ID”,“Display\u Value”,contactTypeLookId),“[请选择]”, 新词典 { {“类”,“验证[必需]输入长度”} })

接触值 是主要的

$(文档).ready(函数(){ var disableFields=$('#clienLocatId').val(); if(disableFields==0){ $('#disable').attr(“隐藏”,false); $('#txtValue').attr(“禁用”,true); $('ddlContact').attr(“disabled”,true); $('chksaveiprimary').attr(“禁用”,真); } 否则{ $('#disable').attr(“隐藏”,true); $('#txtValue').attr(“禁用”,false); $('#ddlContact').attr(“disabled”,false); $('chksaveiprimary').attr(“禁用”,false); } });

} 控制器: 公共操作结果ClientLocationSave(FormCollection FormCollection) { String msg=String.Empty; String newClientLocationId=String.Empty; 字符串clientId=formCollection[“clientId”]; 字符串clientLocId=formCollection[“clientLocId”]; String locationName=formCollection[“txtName”]; 字符串address1=formCollection[“txtAddress1”]; 字符串地址2=formCollection[“txtAddress2”]; 字符串city=formCollection[“txtCity”]; 字符串状态=formCollection[“ddlState”]; 字符串zipCode=formCollection[“txtZipCode”]; 客户端位置clientLoc=新客户端位置(); 尝试 { if(String.IsNullOrWhiteSpace(clientLocId)| | clientLocId==“0”) { clientLoc.ClientID=Convert.ToInt32(ClientID); clientLoc.Name=locationName.Trim(); clientLoc.Address_Line1=地址1; clientLoc.Address_Line2=地址2; clientLoc.City=城市; clientLoc.State\u 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.客户地址。添加(客户地址); } 其他的 { int id=Convert.ToInt32(clientLocId); clientLoc=(从db.Client_位置中的 其中a.ID==ID 选择一个.SingleOrDefault(); clientLoc.Name=locationName.Trim(); clientLoc.Address_Line1=地址1; clientLoc.Address_Line2=地址2; clientLoc.City=城市; clientLoc.State\u LookID=Convert.ToInt32(State); clientLoc.ZipCode=ZipCode; clientLoc.DateModified=DateTime.UtcNow; clientLoc.ModifiedBy=User.Identity.Name; } } 捕获(例外) { msg=“保存失败”; } db.SaveChanges(); if(String.IsNullOrWhiteSpace((msg))) {TempData[“message”]=“已成功保存客户端位置。”;} 否则如果(msg!=“”) {TempData[“message”]=msg;} newClientLocationId=clientLoc.ID.ToString