当在ASP.Net中提交表单时,若ActionResult方法接收到的参数值为null,则出现获取null值的问题。这通常是由于未正确命名表单字段或未使用正确的Http请求方法导致的。以下是解决该问题的一些代码示例:
视图页面中的表单字段名称:
ActionResult方法:
[HttpPost]
public ActionResult YourActionName(string myParameter)
{
//myParameter不为空
//执行操作
}
在Html中使用“HttpPost”:
在控制器中使用“HttpGet”:
[HttpGet]
public ActionResult YourActionName()
{
//执行操作
}
应该正确使用HttpGet或HttpPost。
ActionResult方法中:
[HttpPost]
public ActionResult YourActionName(string myParameter)
{
if(myParameter != null)
{
//执行操作
}
else
{
//提交值为空
}
}
通过以上方式,可以消除获取null值的问题。