0

我正在使用相同的 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 新手,所以如果这是一个微不足道的更改,我深表歉意。

4

2 回答 2

0

控制器

[HttpPost]  
public ActionResult GetMyValue(string custid) 
{          
    var cust = _client.GetCustomerByCustomerId(Guid.Parse(custid));  
    return Json(String.Format("{0:C}", Decimal.Parse(cust.TotalBalance)), JsonRequestBehavior.AllowGet);
}

脚本

<script>
    $(document).ready(function() {
        $("#AccountId").change(function(){
            var val=$(this).val();
            $.ajax({
                url: '@Url.Action("GetMyValue","GasOrder")',
                type: 'POST',
                data: val,
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                error: function (xhr) {
                    alert('Error: ' + xhr.statusText);
                },
                success: function (result) {
                    $("#contactNumber").val(result);
                }
           });
        });
    });
</script>
于 2012-12-07T15:35:08.747 回答
0

因为 .load 函数“将匹配元素的 HTML 内容设置为返回的数据”(http://api.jquery.com/load/)。但是要更改输入文本,您需要更改 value 属性。要解决此问题,您可以 1)从 GetMyValue 操作返回 JSON 2)使用 .post(如果使用 .get,您可能会遇到缓存问题)而不是 .load 并在成功回调中从响应中设置值。就像是

$.post("@Url.Action("GetMyValue","GasOrder")",  { custid : val }, function(r){
    $("#contactNumber").val(r.ContactNumber);
});
于 2012-12-06T22:54:54.783 回答