I have an MVC controller that accepts two string parameters but I will only ever use one or the other. I'm not sure how to best handle the situation. I need to find a way to pass a NULL for which ever parameter will not be used. The way I have it set up now passes the parameters get to the Action but the unused parameter is empty, I need it to be NULL and everything will work as I need. I know I could do an "if" statement and build the @URL.Action link dynamically but that seems clunky. I also saw some posts that suggest a custom route but I would like to hear from users here before I go that route. There must be an easier way.
我有一个接受两个字符串参数的MVC控制器,但我只会使用其中一个。我不知道如何最好地处理这种情况。我需要找到一种方法来传递一个NULL,以便不使用任何参数。我设置的方式现在将参数传递给Action,但未使用的参数为空,我需要它为NULL,一切都可以正常工作。我知道我可以做一个“if”语句并动态构建@ URL.Action链接,但这看起来很笨拙。我还看到了一些建议自定义路线的帖子,但我希望在我走这条路线之前听取用户的意见。必须有一个更简单的方法。
My function to route to the new URL:
我的路由到新网址的功能:
$('#poBtn').on('click', function (e) {
var poId = $('#PoLabel').val();
var reqId = $('#ReqLabel').val();
var url = '@Url.Action("ShipmentsByPo", "Shipping", new {po = "_poId_" , reqId = "_reqId_" })';
url = url.replace('_poId_', poId);
url = url.replace('_reqId_', reqId);
window.location.href = url;Action
})
Action:
行动:
public IEnumerable<ShippingHeaderVM> ShipmentsByPo(string po, string reqID )
{
object[] parameters = { po, reqID };
var shipmentsByPo = context.Database.SqlQuery<ShippingHeaderVM>("spSelectLOG_ShippingNoticeByPo {0},{1}",parameters);
return shipmentsByPo.ToList();
}
}
1 个解决方案
#1
1
One possible solution would be to check inside the controller action if a parameter is empty, set it to null before using it.
一种可能的解决方案是在参数为空时检查控制器操作内部,在使用它之前将其设置为null。
Another possibility is to compose one or the other url on the client:
另一种可能性是在客户端上编写一个或另一个URL:
$('#poBtn').on('click', function (e) {
e.preventDefault();
var poId = $('#PoLabel').val();
var reqId = $('#ReqLabel').val();
var url = '@Url.Action("ShipmentsByPo", "Shipping", new { po = "_poId_" })';
if (poId != '') { // Might need to adjust the condition based on your requirements
url = url.replace('_poId_', encodeURIComponent(poId));
} else {
url = '@Url.Action("ShipmentsByPo", "Shipping", new { reqId = "_reqId_" })';
url = url.replace('_reqId_', encodeURIComponent(reqId));
}
window.location.href = url;
});
#1
1
One possible solution would be to check inside the controller action if a parameter is empty, set it to null before using it.
一种可能的解决方案是在参数为空时检查控制器操作内部,在使用它之前将其设置为null。
Another possibility is to compose one or the other url on the client:
另一种可能性是在客户端上编写一个或另一个URL:
$('#poBtn').on('click', function (e) {
e.preventDefault();
var poId = $('#PoLabel').val();
var reqId = $('#ReqLabel').val();
var url = '@Url.Action("ShipmentsByPo", "Shipping", new { po = "_poId_" })';
if (poId != '') { // Might need to adjust the condition based on your requirements
url = url.replace('_poId_', encodeURIComponent(poId));
} else {
url = '@Url.Action("ShipmentsByPo", "Shipping", new { reqId = "_reqId_" })';
url = url.replace('_reqId_', encodeURIComponent(reqId));
}
window.location.href = url;
});