Perhaps the provision of two forms? asp.net mvc

I have two forms on one page, and both have a save button, respectively. Whenever I click another button, I want the changes that I added in another form to be saved as well.

This is my code:

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

Can this be done using jQuery, and if so, how?

+4
source share
1 answer

Allows you to slightly change your question to something more abstract to help you:

You have two paper forms that you must sign and provide to your manager. One must be provided to Fred in HR, the other to Wilma in Sale on the other side of the building.

Can you leave your table and both of you go right away? Of course not. You need to first choose the one you want to do, and then go to the second, before finally arriving at your manager with both signed forms.

The same is true for your page, you can link all this into one form and send it to the server, process the first part, then get another code to process the second part, and then return the result to the user.

As long as there is some bizarre trick that you can use to get around this, you need to ask yourself why you want to. If you always keep both forms, then why worry about having two?

+2
source

Source: https://habr.com/ru/post/1493111/


All Articles