我在一个页面中有两个表单,并且都分别有一个保存按钮。每当我单击另一个按钮时,我希望我在另一个表单上添加的更改也能够保存。
这是我的代码:
<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 来完成吗,如果是的话 - 怎么做?