Javascript对象有数据,但没有在MVC视图中填充文本框
本文关键字:视图 填充 文本 MVC 数据 对象 Javascript | 更新日期: 2023-09-27 18:08:50
我有一个方法从我的控制器传递数据作为json,当我通过javascript的步骤,我看到数据使它到javascript对象,但由于某种原因,它没有填充我的文本框。当我在Chrome中调试时,我看到对象中的数据并且没有抛出错误。
一开始我试着这样做…
public ActionResult Preferences()
{
GetClientResult();
return View();
}
但我认为可能是问题,所以我改变了周围的东西,现在看起来像这样
在我的控制器中我有…
public JsonResult GetClientResult()
{
OQOE = new OQOEDAL();
List<ClientInformation> ClientList = OQOE.GetClientInformation();
return Json(ClientList, JsonRequestBehavior.AllowGet);
}
和我的javascript文件看起来像这样…
$(document).ready(function () {
GetClientInformation();
});
function GetClientInformation() {
$.ajax({
type: "GET",
url: AddClientURLParam.AddGetClientInformationURL,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data, textStatus, jqXHR) {
return DataToGet(data);
}
})
}
function DataToGet(clientInfoData) {
$("#txtCompanyName").val(clientInfoData[0].customerName);
$("#txtAddress1").val(clientInfoData[0].address1);
$("#txtAddress2").val(clientInfoData[0].address2);
$("#txtCity").val(clientInfoData[0].city);
$("#txtState").val(clientInfoData[0].state);
$("#txtZip").val(clientInfoData[0].zip);
$("#txtEmail").val(clientInfoData[0].email);
$("#txtContact").val(clientInfoData[0].contactName);
$("#txtPhone").val(clientInfoData[0].phone);
$("#txtWorkPhone").val(clientInfoData[0].work);
$("#txtMobile").val(clientInfoData[0].mobile);
$("#txtFax").val(clientInfoData[0].fax);
$("#txtOther").val(clientInfoData[0].other);
}
和我的视图有这个…
<div class="tab-pane active" id="YourInfo">
<h4>Please fill out your Information below.</h4>
<div class="form-group">
<label for="txtCompanyName" class="control-label col-md-2"><b>Company Name:</b></label>
<div class="col-md-8">
<input id="txtCompanyName" type="text" class="form-control max-size" required />
</div>
</div>
<hr />
<div class="col-md-6">
<fieldset>
<legend><h4><b>Address</b></h4></legend>
<div class="form-group">
<label for="txtAddress1" class="control-label col-md-2">Address</label>
<div class="col-md-10">
<input id="txtAddress1" type="text" class="form-control max-size" />
</div>
</div>
<div class="form-group">
<label for="txtAddress2" class="control-label col-md-2">Address2</label>
<div class="col-md-10">
<input id="txtAddress2" type="text" class="form-control max-size" />
</div>
</div>
<div class="form-group">
<label for="txtCity" class="control-label col-md-2">City</label>
<div class="col-md-10">
<input id="txtCity" type="text" class="form-control max-size" />
</div>
</div>
<div class="form-group">
<label for="txtState" class="control-label col-md-2">State</label>
<div class="col-md-4">
<input id="txtState" type="text" class="form-control" />
</div>
<label for="txtZip" class="control-label col-md-2">Zip/Postal</label>
<div class="col-md-4">
<input id="txtZip" type="text" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="txtEmail" class="control-label col-md-2">Email</label>
<div class="col-md-10">
<input id="txtEmail" type="text" class="form-control max-size" />
</div>
</div>
</fieldset>
</div>
<div class="col-md-6">
<fieldset>
<legend><h4><b>Contact</b></h4></legend>
<div class="form-group">
<label for="txtContact" class="control-label col-md-2">Contact</label>
<div class="col-md-10">
<input id="txtContact" type="text" class="form-control max-size" />
</div>
</div>
<div class="form-group">
<label for="txtPhone" class="control-label col-md-2">Phone</label>
<div class="col-md-10">
<input id="txtPhone" type="text" class="form-control max-size" />
</div>
</div>
<div class="form-group">
<label for="txtWorkPhone" class="control-label col-md-2">Work</label>
<div class="col-md-10">
<input id="txtWorkPhone" type="text" class="form-control max-size" />
</div>
</div>
<div class="form-group">
<label for="txtFax" class="control-label col-md-2">Fax</label>
<div class="col-md-10">
<input id="txtFax" type="text" class="form-control max-size" />
</div>
</div>
<div class="form-group">
<label for="txtMobile" class="control-label col-md-2">Mobile</label>
<div class="col-md-10">
<input id="txtMobile" type="text" class="form-control max-size" />
</div>
</div>
<div class="form-group">
<label for="txtOther" class="control-label col-md-2">Other</label>
<div class="col-md-10">
<input id="txtOther" type="text" class="form-control max-size" />
</div>
</div>
</fieldset>
</div>
<div class="form-group">
<div class="col-md-4 col-md-offset-10">
<input id="btnSaveClientInfo" type="button" value="Save" class="btn btn-primary" />
<input id="btnCancelClientInfo" type="button" value="Cancel" class="btn btn-primary" />
</div>
</div>
</div>
嗯,似乎这个问题将通过从AJAX success
处理程序中删除关键字return
来解决。在这种情况下,函数DataToGet
不返回任何有意义的东西。它只是填充输入值。所以。我的答案是基于评论辩论