我正在使用相同的 javascript 作为标签,所以我不确定提交字段有什么问题。
这是我的看法:
@model SuburbanCustPortal.Models.OrderGasModel
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script>
$(function () {
$("#datepicker").datepicker(); });
</script>
<script>
$(function(){
$("#AccountId").change(function(){
var val=$(this).val();
$("#contactNumber").load("@Url.Action("GetMyValue","GasOrder")", { custid : val });
document.forms[0].Amount.focus(); }); });
</script>
@{
ViewBag.Title = "Order Gas"; }
<h2>Order Gas</h2>
@using (Html.BeginForm("OrderGasSuccess", "GasOrder", FormMethod.Post))
{
@Html.ValidationSummary(true, "Submit was unsuccessful. Please correct the errors and try again.")
<div>
<fieldset>
<legend>Account Information - all fields required</legend>
<div class="editor-label">
@Html.LabelFor(m => m.AccountNumber)
</div>
<div class="editor-field">
@Html.DropDownListFor(x => x.AccountId, (IEnumerable<SelectListItem>)ViewBag.Accounts)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.TankPercent)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.TankPercent, new { @class = "GenericTextBox" })
@Html.ValidationMessageFor(m => m.TankPercent)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Amount)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Amount, new { @class = "GenericTextBox" })
@Html.ValidationMessageFor(m => m.Amount)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.DateRequested)
</div>
<div class="editor-field">
<input type="text" id="datepicker" class="GenericTextBox"/>
@Html.ValidationMessageFor(m => m.DateRequested)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.ContactNumber)
</div>
<div class="editor-field">
<input type="text" id="contactNumber" class="GenericTextBox"/>
@Html.ValidationMessageFor(m => m.ContactNumber)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Message)
</div>
<div class="editor-field">
@Html.EditorFor(x => x.Message)
</div>
<div>
<input type="submit" value="Submit" />
</div>
</fieldset>
</div>
}
任何我的方法被调用:
public string GetMyValue(string custid)
{
var cust = _client.GetCustomerByCustomerId(Guid.Parse(custid));
return String.Format("{0:C}", Decimal.Parse(cust.TotalBalance));
}
正在调用 GetMyValue 方法并返回一个值。下拉列表也填充了值。
当我更改 ddl 中的值时,GetMyValue 会触发,但输入框不会获取该值。
有人看到我做错了吗?
==== 更新 ====
我按照以下建议进行了更改。我不确定我是否做对了,因为我收到了这条消息:
Microsoft JScript 运行时错误:“val”未定义
这是我对更改的看法:
@model SuburbanCustPortal.Models.OrderGasModel
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script>
$(function () {
$("#datepicker").datepicker();
});
</script>
<script>
$.post("@Url.Action("GetMyValue","GasOrder")", { custid : val }, function(r){
$("#contactNumber").val(r.ContactNumber);
});
</script>
@{
ViewBag.Title = "Order Gas";
}
<h2>Order Gas</h2>
@using (Html.BeginForm("OrderGasSuccess", "GasOrder", FormMethod.Post))
{
@Html.ValidationSummary(true, "Submit was unsuccessful. Please correct the errors and try again.")
<div>
<fieldset>
<legend>Account Information - all fields required</legend>
<div class="editor-label">
@Html.LabelFor(m => m.AccountNumber)
</div>
<div class="editor-field">
@Html.DropDownListFor(x => x.AccountId, (IEnumerable<SelectListItem>)ViewBag.Accounts)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.TankPercent)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.TankPercent, new { @class = "GenericTextBox" })
@Html.ValidationMessageFor(m => m.TankPercent)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Amount)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Amount, new { @class = "GenericTextBox" })
@Html.ValidationMessageFor(m => m.Amount)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.DateRequested)
</div>
<div class="editor-field">
<input type="text" id="datepicker" class="GenericTextBox"/>
@Html.ValidationMessageFor(m => m.DateRequested)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.ContactNumber)
</div>
<div class="editor-field">
<input type="text" id="contactNumber" class="GenericTextBox"/>
@Html.ValidationMessageFor(m => m.ContactNumber)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Message)
</div>
<div class="editor-field">
@Html.EditorFor(x => x.Message)
</div>
<div>
<input type="submit" value="Submit" />
</div>
</fieldset>
</div>
}
我是 javascript 新手,所以如果这是一个微不足道的更改,我深表歉意。