我正在使用GridMVC在我的一個網格列中呈現按鈕,以充當資產驗證按鈕。基本上,單擊按鈕時,我想根據ID在數據庫中找到相關資產,並將[verified_date]
設置爲DateTime.Now
。Ajax POST對參數[assetID]有空條目?
columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => @<a href="#" class="btn btn-default btn-sm noDecoration" onclick="verifyAsset(@o.Id)"><span class="glyphicon glyphicon-ok"></span> @*View*@</a>).SetWidth(15);
在上述使用超鏈接標籤,我打電話給我的verifyAsset()
javascript函數:
@section Scripts {
<script type="text/javascript">
function verifyAsset(assetID) {
alert("The assetID is: " + assetID);
var data = { verified_date: assetID };
alert("Got this far...");
$.ajax({
type: "POST",
dataType: "JSON",
url: '@Url.Action("verifyAsset", "INV_Assets")',
data: data,
success: function (resp) {
alert("Success! Asset " + resp.ID + " successfully verified on " + resp.VDate);
},
error: function (resp) {
alert("There was an error verifying this Asset...");
}
});
}
</script>
}
當我嘗試通過funcitonality雖然跑,我ajax
總是error
ING和斷點我的控制器動作verifyAsset()
永遠不會被達到:在我返回
[HttpPost]
public JsonResult verifyAsset(int assetID)
{
INV_Assets asset = db.INV_Assets.Find(assetID);
asset.verified_date = DateTime.Now;
try
{
if (ModelState.IsValid)
{
db.SaveChanges();
}
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
return Json(new { ID = asset.Id, VDate = asset.verified_date }, JsonRequestBehavior.AllowGet);
}
錯誤日誌是:The parameters dictionary contains a null entry for parameter 'assetID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.JsonResult verifyAsset(Int32)' in 'InventoryTracker.Controllers.INV_AssetsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
。
任何人都可以發現我可能會出錯的地方嗎?在我的劇本的第一alert()
我已經證實了正確ID
值被傳遞到可變assetID
的功能,然而,我發現,如果我嘗試設置data
爲:var data = { verified_date: @Convert.ToInt32(assetID) };
的assetID
標誌爲not exist in the current context
...?
編輯:
下面是類似的代碼我得到工作之前:
腳本:
$('#submitNewModel').click(function() {
var form = $(this).closest('form');
var data = { description: document.getElementById('textNewModel').value };
//var data = { model_description: document.getElementById('textNewModel').value, created_date: @DateTime.Now, created_by: @System.Environment.UserName };
$.ajax({
type: "POST",
dataType: "JSON",
url: '@Url.Action("createNewModel", "INV_Assets")',
data: data,
success: function (resp) {
$('#selectModel').append($('<option></option>').val(resp.ID).text(resp.Text));
form[0].reset();
$('#createModelFormContainer').hide();
},
error: function() {
alert("ERROR!");
}
});
});
INV_AssetsController - createNewModel():
[HttpPost]
public JsonResult createNewModel(string description)
{
INV_Models model = new INV_Models()
{
// ID auto-set during save.
model_description = description,
created_date = DateTime.Now,
created_by = System.Environment.UserName
};
try
{
if (ModelState.IsValid)
{
db.INV_Models.Add(model);
db.SaveChanges();
}
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
return Json(new { ID = model.Id, Text = model.model_description }, JsonRequestBehavior.AllowGet);
}
審查gutsmania
的回答後,我意識到他確實是正確的,我精神上偷步宣佈我data
現場路過的verified_date
當所有我正打算是把ID
值傳遞給預期的參數在我的控制器行動。由於verification_date
不等於assetID
(預期的參數),因此該操作從未被實際調用,並且失敗爲NULL
。
更新代碼:
SCRIPT:
<script type="text/javascript">
function verifyAsset(assetID) {
alert("The assetID is: " + assetID);
var data = { asset_ID: assetID };
alert("Got this far...");
$.ajax({
type: "POST",
dataType: "JSON",
url: '@Url.Action("verifyAsset", "INV_Assets")',
data: data,
success: function (resp) {
alert("Success! Asset " + resp.ID + " successfully verified on " + resp.VDate);
},
error: function (resp) {
alert("There was an error verifying this Asset...");
}
});
}
</script>
INV_AssetsController - verifyAssets():
[HttpPost]
public JsonResult verifyAsset(int asset_ID)
{
INV_Assets asset = db.INV_Assets.Find(asset_ID);
asset.verified_date = DateTime.Now;
try
{
if (ModelState.IsValid)
{
db.SaveChanges();
}
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
return Json(new { ID = asset.Id, VDate = asset.verified_date }, JsonRequestBehavior.AllowGet);
}
你是對的! (見上面的編輯)。唯一好奇的是,我最後一次警報'alert(「Success!Asset」+ resp.ID +「已在」+ resp.VDate「上成功驗證);'returns'成功!資產1成功驗證/日期(1424805494397)/',但無論如何它會在我的數據庫中保存'2/24/2015 1:18:14 PM',所以一切都很好。你偶然想到我可以修改(成功)我的按鈕的顏色從「默認」更改爲綠色「成功」類嗎?理想情況下,我希望將整個表單設置爲加載狀態,以顯示在過去3個月內驗證過的爲綠色,除此之外,其他所有表單均爲默認值。 – 2015-02-24 19:28:52
這樣的事情? 成功:function(){('#myButton')。addClass('success'); } – gutsmania 2015-02-24 21:12:55