可选参数必须是引用类型,可空类型,或者声明为可选参数

时间:2015-06-17 17:29:33

标签: c# asp.net-mvc

我使用ViewBag将一个值从控制器传递给View并将其存储在变量中。然后从同一视图我将值发送回存储在变量中的Controller。早些时候它曾经工作,但最近它开始给我低于错误。

  

参数字典包含非可空类型'System.Int32'参数'nServiceId'的空条目'非可空类型'System.Int32'用于方法   'System.Web.Mvc.ActionResult SaveRequest(Int32,BussinessObjects.class1,   System.Web.Mvc.FormCollection)”。可选参数必须是a   引用类型,可空类型,或声明为可选   参数。

我的代码如下:

控制器:

public ActionResult EnterData(int nServiceId)
{
    ViewBag.ServiceId = nServiceId;
    return View();
}

[HttpPost]
public ActionResult SaveRequest(int nServiceId, GetQuoteInfo viewModel, FormCollection fCollection)
{

}

查看:

@{int nServiceId = ViewBag.ServiceId;}
@using (Html.BeginForm("SaveRequest",
    "MyController",
    FormMethod.Post,
    new
    {
        name = "EnterData",
        id = "EnterData",
        nServiceId = nServiceId
    }))

1 个答案:

答案 0 :(得分:3)

您没有将nServiceId的值传递给控制器​​。您正在使用Html.BeginForm() (new { name = "EnterData", id = "EnterData", nServiceId = nServiceId },其中最后一个参数nServiceId = "nServiceId")将@using (Html.BeginForm("SaveRequest", "MyController", new { nServiceId = nServiceId }, FormMethod.Post, new { name = "EnterData", id = "EnterData" })) 添加为html属性,而不是路由值(检查它生成的html)。

您需要使用this overload,其用途为

new { nServiceId = ViewBag.ServiceId }

ProcessBuilder pb = new ProcessBuilder("sftp","-oIdentityFile=privateKey","-b","commands.txt","username@hostname");

相关问题