Asp.net MVC, 誰吃了我的”id”參數值?

來源:互聯網
上載者:User
話說我有一個action: public void SaveTask(Guid id, string title, bool? isPublic)

準備讓它接收"CreateTask"和"EditTask"建立的表單:    <form action="SaveTask" method="post">
        <dl>
            <dt>標題</dt>
            <dd><input type="text"  value=""  size="20"  name="title"  id="title"  /></dd>
            <dt>公開</dt>
          <dd><input type="checkbox"  value="true"  name="isPublic"  />&nbsp;true</dd>
        </dl>
       <input type="hidden"  value="00000000-0000-0000-0000-000000000000"  name="id"  id="id"  />
        <input type="submit"  value="建立任務"  name="submit"  id="submit"  />
    </form>

但是每次提交表單,都會出現異常:[FormatException: GUID 應包含帶 4 個短劃線的 32 位元(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。]
   System.Guid..ctor(String g) +2284
   System.ComponentModel.GuidConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) +103
   System.ComponentModel.TypeConverter.ConvertFrom(Object value) +54
   System.Web.Mvc.Controller.ConvertParameterType(Object value, Type destinationType, String parameterName, String actionName) +161

[InvalidOperationException: Cannot convert parameter 'id' in action 'SaveTask' with value '' to type 'System.Guid'.]
   System.Web.Mvc.Controller.ConvertParameterType(Object value, Type destinationType, String parameterName, String actionName) +301
   System.Web.Mvc.Controller.InvokeActionMethod(MethodInfo methodInfo, RouteValueDictionary values) +760

[InvalidOperationException: A value is required for parameter 'id' in action 'SaveTask'. The parameter either has no value or its value could not be converted. To make a parameter optional its type should either be a reference type or a Nullable type.]
   System.Web.Mvc.Controller.InvokeActionMethod(MethodInfo methodInfo, RouteValueDictionary values) +902
   System.Web.Mvc.Controller.InvokeAction(String actionName, RouteValueDictionary values) +263
   System.Web.Mvc.Controller.Execute(ControllerContext controllerContext) +161
   System.Web.Mvc.Controller.System.Web.Mvc.IController.Execute(ControllerContext controllerContext) +4
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +212
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +36
   System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +4
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +358
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64

注意加底線的部分:不是明明有回傳的值"00000000-0000-0000-0000-000000000000"嗎,怎麼變成了Null 字元串了呢?誰吃了我的參數值?
  經過兩個小時艱苦卓絕的調查取證(其中包含了到處搜尋Asp.net MVC preview 2源碼,以及試圖使用Reflector重建源項目未遂),最後靈機一動,把"id"改成"taskId",於是就一切正常了.

  結論很顯然,"id"這個參數名在預設情況下是受到特殊對待的,只能在URL裡附帶,不能post..

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.