因为我的生活无法弄清楚如何从我的控制器中显示我从'cics'对象返回的值。当我调试控制器时,它返回值。但我似乎无法在ajax调用中弄清楚我的'Success'方法。有人可以帮我解决我出错的地方吗?此外,这可以有多个产品,所以我试图保持它特定于该元素。任何帮助将不胜感激。
function FillProductbyupc(el) {
var val = el.value;
var container = $(el).parent().parent();
var pcics = container.children(".cics");
var elements = document.getElementsByClassName('product');
$.ajax({
url: '/Case/GetData/',
type: "GET",
dataType: "JSON",
data: "upc=" + val,
success: function (cics) {
//container.cics.val(cics.CorpItemCode);
$("#upc").html(""); // clear before appending new list
$("#corpitemcode").html(""); // clear before appending new list
$("#itemdesc").html(""); // clear before appending new list
$("#corpitemcode").append(
$('#corpitemcode').val(cics.CorpItemCode));
$("#itemdesc").append(
$('#itemdesc').val(cics.ItemDsc));
},
error: function () {
alert("UPC does not exist");
}
});
}
这是我的产品。
<div class="product">
<div class="alert alert-info" role="alert">
<div class="row">
<div class="form-group">
<div class="col-md-2">
@Html.LabelFor(x => x.UPC)<small><span class="glyphicon glyphicon-asterisk text-danger"></span></small>
@Html.TextBoxFor(x => x.UPC, null, new { @class = "form-control produpc", placeholder = Html.DisplayNameFor(x => x.UPC), @onblur = "FillProductbyupc(this)" })
@Html.ValidationMessageFor(x => x.UPC, "", new { @class = "text-danger" })
</div>
<div class="col-md-2">
@Html.LabelFor(x => x.PLU)<small><span class="glyphicon glyphicon-asterisk text-danger"></span></small>
@Html.TextBoxFor(x => x.PLU, null, new { @class = "form-control prodplu", placeholder = Html.DisplayNameFor(x => x.PLU) })
@Html.ValidationMessageFor(x => x.PLU, "", new { @class = "text-danger" })
</div>
<div class="col-md-2">
<label>CIC</label>
@Html.TextBoxFor(p => p.CIC, new { @class = "form-control prodcic" })
</div>
<div class="col-md-2">
<label>Desc</label>
@Html.TextBoxFor(p => p.Description, new { @class = "form-control proddesc" })
</div>
<div class="col-md-1">
<label>Size</label>
@Html.TextBoxFor(p => p.Size, new { @class = "form-control prodsize" })
</div>
<div class="col-md-1">
<label>Shelf Price</label>
@Html.TextBoxFor(p => p.ShelfPrice, new { @class = "form-control prodprice" })
</div>
<div class="col-md-1">
<label>Like CIC</label>
@Html.TextBoxFor(p => p.LikeCIC, new { @class = "form-control prodlikecic" })
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
@Html.RemoveLink("Remove", "div.product", "input.mark-for-delete")
</div>
</div>
</div>
控制器。
public JsonResult GetData(string upc, decimal? corpitemcode)
{
if (!string.IsNullOrEmpty(upc))
{
upc = upc.Replace("-", "");
}
CICS cics = new CICS();
cics = CICSManager.GetbyUPCorCorp(upc, corpitemcode);
return Json(cics, JsonRequestBehavior.AllowGet);
}
工作职能
function FillProductbyupc(el) {
var val = el.value;
var container = $(el).parent().parent();
var pcics = $(container).find('.prodcic');
var pdesc = $(container).find('.itemdsc');
$.ajax({
url: '/Case/GetData/',
type: "GET",
dataType: "JSON",
data: "upc=" + val,
success: function (cics) {
pcics.val(cics.CorpItemCode);
pdesc.val(cics.ItemDsc);
},
error: function () {
alert("UPC does not exist");
}
});
}
答案 0 :(得分:0)
html()
和.append()
用于添加到HTML节点。我怀疑你想要的是:
success: function (cics) {
//container.cics.val(cics.CorpItemCode);
$("#upc").val("");
$("#corpitemcode").val(cics.CorpItemCode);
$("#itemdesc").val(cics.ItemDsc);
},
在插入新HTML之前无需清除。