javascript - Referencing and Populating Fields of Type 'Model' in AJAX Post -
how 1 use jquery reference field(s) of model serving variable of parent model, retrieve fields different entries of inner model?
for example, there's accountownermodel , accountmodel, latter containing variable of accountownermodel type.
accountowner model:
public class accountownermodel { public int accountownerid {get; set;} public string accountownername {get; set;} public string accountowneremail {get; set;} public string accountownerphonenumber {get; set;} }
account model:
public class accountmodel { public int accountid {get; set;} public int accountownerid {get; set;} public accountownermodel accountowner {get; set;} }
in controller, there action retrieve accountowner fields should different accountowner selected:
[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); }
in view, there's dropdownlist accountownermodels accountownerid , accountownername, lines show , modify data accountownerid, save different accountownermodel accountmodel, , fetch data different selected 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>
so, more specific, can variables in jquery code assigned $("#accountowner.accountownername"), or period refer class id? also, ajax post syntax correct? code isn't showing errors in either visual studio or chrome console, isn't returning on different dropdown selections, either. i've tried referring variables field name, e.g. $("accountownername"), searching site no avail, , using $("#accountownerid").val() in ajax post data, null each time seemed @ least send null on each dropdown change. there's id each column class, using in references affect "@labelfor" well? in advance!
the actual answer came sister question here: generated html referring elements of type model ("#model_element"), or ("#accountowner_accountownername") "m.accountowner.accountownername" in situation, , did not require syntax escaping period in jquery selector.
Comments
Post a Comment