gpt4 book ai didi

javascript - 可以提交两个表格吗?网络MVC

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:18:44 26 4
gpt4 key购买 nike

我在一个页面中有两个表单,并且都分别有一个保存按钮。每当我单击另一个按钮时,我都希望我在另一个表单上添加的更改也被保存。

这是我的代码:

<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 完成吗?如果可以,怎么做?

最佳答案

让我们将您的问题改写成更抽象的内容以帮助您:

您有两份纸质表格需要签名并交给您的经理。一个必须给人力资源部的弗雷德,另一个给大楼另一侧销售部的威尔玛。

你能离开你的办公 table 并同时签名吗?当然不是。您需要先选择一个做,然后再去做第二个,最后在两份表格都签名后到达您的经理处。

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

虽然您可以使用一些花哨的技巧来解决这个问题,但您需要问问自己为什么要这么做。如果您总是保存两种形式,那么为什么还要保存两种形式呢?

关于javascript - 可以提交两个表格吗?网络MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17826270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com