我正在尝试构建window.location.href,传递一个1项查询字符串。
MVC操作方法要求该条目是一个整数。
我可以硬编码一个'2',它是有效的。我当然不想硬编码。
因此,我获取值并设置一个变量,并尝试使用以下两种情况中的任何一种来传递该变量,但失败了。我得到:
{0}The parameters dictionary contains a null entry for parameter 'blogCategoryId' of non-nullable type
'System.Int32' for method 'System.Web.Mvc.ActionResult BlogsForMaintIndex(Int32)' in
'GbngWebClient.Controllers.AdminFunctions.BlogMaint.BlogMaintController'. An optional parameter must
be a reference type, a nullable type, or be declared as an optional parameter.
如何正确构建它?
这是JavaScript:
$(function () {
$('#buttonClose').on('click', function () {
$('#modalView').modal('hide');
// The action method expects an integer.
var blogCategoryId = @Convert.ToInt32(Session["BlogCategoryId"]);
alert('blogCategoryId: ' + blogCategoryId);
// WORKS!
//window.location.href = "/BlogMaint/BlogsForMaintIndex/?blogCategoryId=2";
// Neither of these 2 work.
//window.location.href = "/BlogMaint/BlogsForMaintIndex/?blogCategoryId=blogCategoryId";
window.location.href = "/BlogMaint/BlogsForMaintIndex/?blogCategoryId=' + blogCategoryId + '";
})
})
以下是简化的操作方法:
[HttpGet]
public ActionResult BlogsForMaintIndex(int blogCategoryId)
{
}
试试这个
let myurl = "/BlogMaint/BlogsForMaintIndex/?blogCategoryId=" + blogCategoryId;
window.location.href = myurl;