引用和填充类型字段'模型'在AJAX Post中

时间:2015-08-19 16:24:30

标签: javascript jquery ajax asp.net-mvc

如何使用jQuery引用模型的字段作为父模型的变量,然后为该内部模型的不同条目检索这些字段?

例如,有一个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,则会有一个Action来检索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);
}

在View中,AccountOwnerMode和AccountOwnerName会显示AccountOwnerModel的下拉列表,然后显示和修改该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代码中的变量是否可以通过$(&#34;#AccountOwner.AccountOwnerName&#34;)来分配,或者句点是否会引用类ID?另外,AJAX Post语法是否正确?此代码不会在Visual Studio或Chrome控制台中显示错误,但它也不会在不同的下拉列表选项中返回任何内容。我已经尝试将变量引用到字段名称,例如$(&#34; AccountOwnerName&#34;),搜索此网站无效,并在AJAX Post数据中使用$(&#34;#AccountOwnerID&#34;)。val(),每次都是null但似乎至少在每个下拉列表更改时发送一个null。每个列类都有一个id,但是在引用中使用它会影响&#34; @ LabelFor&#34;还有?提前谢谢!

1 个答案:

答案 0 :(得分:0)

对此的实际答案来自姐妹问题here:生成的HTML指的是Model类型的元素(&#34;#Model_Element&#34;)或(&#34;# AccountOwner_AccountOwnerName&#34;)用于&#34; m.AccountOwner.AccountOwnerName&#34;在这种情况下,并不需要在jQuery选择器中转义句点的语法。