1
null以下位代码应该检索任何具体的价格日期/价格日期ID的记录价格。除了传递给Controller的值总是为null,“undefined”,“”,或者作为字符串传递时否则缺少一个值,并且不传递/在View中传递错误时一个int。我为这个问题使用了一个模板,但我看不到这里发生了什么。不考虑可能会出现另一个问题的最终计算结果,任何人都可以发现ID值未被传递的原因吗?提前致谢!AJAX获取呼叫发送到控制器
HTML:
<div class="col-lg-8">
@Html.LabelFor(m => m.PriceDate, "Pricing Date")
@Html.DropDownListFor(m => m.PriceDate, Model.PricingDates, "--New Pricing--")
</div>
脚本:
$(function() {
var $priceValue = $("#PriceDate").val(),
$pID = { iD: $priceValue };
$("#PriceDate").change(function() {
if ($(this).val()) {
//make AJAX call for historical Price data and populate table
$.ajax({
type: "GET",
url: '@Url.Action("GetPrices", "Sales")',
data: $pID,
success: function (data) {
//Fill data
$("#Prices").val(data);
}
});
}
else {
//clear data
$("#Prices").val('');
}
}).change();
});
控制器:
public ActionResult GetPrices(string iD)
{
int priceID;
Int32.TryParse(iD, out priceID);
//priceID = iD;
dbEntities db = new dbEntities();
var selectedPrice = new List<PricesModel>();
var accountPrices = db.uspGetPrices(priceID);
//Do stuff
return Json(selectedPrice, JsonRequestBehavior.AllowGet);
}
生成的HTML:
<div class="col-lg-8">
<label for="PriceDate">Pricing Date</label>
<select data-val="true" data-val-number="The field PriceDate must be a number." data-val-required="The PriceDate field is required." id="PriceDate" name="PriceDate"><option value="">--New Pricing--</option>
<option value="2">1/4/2016 6:33 PM</option></select>
</div>
这样做。我知道这很简单。非常感谢! – jle
我正在研究这个,但是页面DOM加载后不会触发的影响是什么? – jle