如何使用jQuery引用模型的字段作爲父模型的變量,然後檢索那些內部模型的不同條目的字段?在AJAX中引用和填充'Model'類型的字段
例如,有一個AccountOwnerModel和一個AccountModel,後者包含一個AccountOwnerModel類型的變量。
AccountOwner型號:
public class AccountOwnerModel
{
public int AccountOwnerID {get; set;}
public string AccountOwnerName {get; set;}
public string AccountOwnerEmail {get; set;}
public string AccountOwnerPhoneNumber {get; set;}
}
帳戶型號:
public class AccountModel
{
public int AccountID {get; set;}
public int AccountOwnerID {get; set;}
public AccountOwnerModel AccountOwner {get; set;}
}
在Controller中,有一個動作來檢索AccountOwner字段應不同AccountOwner來選擇:
[HttpPost]
public ActionResult GetAccountOwnerInfo(string ownerID)
{
Int64 accountOwnerID = 0;
Int64.TryParse(ownerID, out accountOwnerID);
DbEntities projectDB = new DbEntities();
var owner = projectDB.uspGetAccountOwnerInformation(accountOwnerID).FirstOrDefault();
AccountOwnersModel accountOwner = new AccountOwnersModel()
{
AccountOwnerID = owner.AccountOwnerID,
AccountOwnerName = owner.AccountOwnerName,
AccountOwnerEmail = owner.AccountOwnerEmail,
AccountOwnerPhoneNumber = owner.AccountOwnerPhoneNumber
};
return Json(accountOwner, JsonRequestBehavior.AllowGet);
}
在視圖中,由Acco爲AccountOwnerModels提供了一個下拉列表untOwnerID和AccountOwnerName,然後線顯示和修改數據爲AccountOwnerID,不同AccountOwnerModel保存到AccountModel,並獲取的數據不同的選定AccountOwnerID:
<div class="row">
<div class="col-lg-4">
@Html.LabelFor(m => m.AccountOwnerID, "Account Owner Name", new { @class = "req" })
@Html.DropDownListFor(m => m.AccountOwnerID, Model.AccountOwners, "--New Owner--")
</div>
<div class="col-lg-8" id="AccountOwnerName">
@Html.LabelFor(m => m.AccountOwner.AccountOwnerName, new { @class = "req"})
@Html.TextBoxFor(m => m.AccountOwner.AccountOwnerName)
</div>
</div>
<div class="row">
<div class="col-lg-6" id="AccountOwnerPhoneNumber">
@Html.LabelFor(m => m.AccountOwner.AccountOwnerPhoneNumber, "Owner Phone Number")
@Html.TextBoxFor(m => m.AccountOwner.AccountOwnerPhoneNumber, new { @class = "form-control", data_parsley_required = "true" })
</div>
<div class="col-lg-6" id="AccountOwnerEmailAddress">
@Html.LabelFor(m => m.AccountOwner.AccountOwnerEmail, "Owner E-Mail Address")
@Html.TextBoxFor(m => m.AccountOwner.AccountOwnerEmail, new { @class = "form-control", data_parsley_required = "true" })
</div>
</div>
<div class="row">
<div class="col-lg-12 text-right">
<button type="submit" class="btn btn-success">Save Account Owner</button>
</div>
</div>
@section Scripts{
<script src="~/Scripts/jquery-2.1.3.js"></script>
<script src="/Scripts/jquery-ui-1.11.4.js"></script>
<script>
$("#AccountOwnerID").change(function() {
var $ownerName = $("#AccountOwner.AccountOwnerName"),
$ownerEmail = $("#AccountOwner.AccountOwnerEmail"),
$ownerPhone = $("#AccountOwner.AccountOwnerPhoneNumber");
if (this.value >= 1) {
$.ajax({
type: "POST",
url: '/Sales/GetAccountOwnerInfo',
data: { ownerID: $("#AccountOwnerID").val() },
success: function (data) {
//Fill data
$ownerName.val = data.AccountOwnerName;
$ownerEmail.val = data.AccountOwnerEmail;
$ownerPhone.val = data.AccountOwnerPhoneNumber;
}
});
}
else {
//clear fields
$ownerName.val('');
$ownerEmail.val('');
$ownerPhone.val('');
}
}).change();
</script>
所以,爲了更具體地,可以在jQuery代碼中的變量由$(「#AccountOwner.AccountOwnerName」)賦值,還是該句點引用類ID?此外,AJAX Post語法是否正確?此代碼不會在Visual Studio或Chrome控制檯中顯示錯誤,但它不會在不同的下拉選擇中返回任何內容。我試着將變量引用到字段名稱,例如$(「AccountOwnerName」),搜索這個網站無效,並在AJAX Post數據中使用$(「#AccountOwnerID」)。val(),每次都是空的,但似乎至少在每個下拉列表中發送一個空值。每個列類都有一個id,但是在引用中使用它也會影響「@LabelFor」?提前致謝!生成的HTML指的類型模式的要素(「#Model_Element」)或(「#AccountOwner_AccountOwnerName」)爲「m.AccountOwner.AccountOwnerName:
是元素的ID嗎?它有一段時間嗎? – code
哪個元素?在腳本中,「m.AccountOwnerID」的標識只是$(「#AccountOwnerID」),但我不確定該標識或如何引用帳戶模型中AccountOwnerModel對象的元素,例如「m.AccountOwner.AccountOwnerName」。我在帖子末尾嘗試了各種各樣的東西,但似乎沒有任何成效。我是新來的jQuery,AJAX等,以及可能是一個語法問題? – jle
查看[這個答案](http://stackoverflow.com/a/350300/1260077)在jQuery選擇器中跳過這段時間。 – GvM