2013-06-22 90 views
0

我是ASP.NET MVC的新學習者。這個問題應該很容易回答專業。我試圖提交表單(發表評論),並獲得錯誤。無法解析ASP.NET MVC錯誤

這裏是我的控制器

[HttpPost] 
public ActionResult Index(long id, CommentViewModel comment) { 
    var service = new UserService(); 
    var articleService = new ArticleService(); 
    comment.UserId = service.GetUserByUsername(User.Identity.Name).UserId; 
    comment.ArticleId = id; 
    comment.CommentTime = DateTime.Now; 
    articleService.AddArticleComment(comment); 
    return View(); 
} 

這裏是我的ViewModel

public class CommentViewModel : BaseViewModel { 
    public Int64 CommentId { get; set; } 
    public Int64 UserId { get; set; } 
    public Int64 ArticleId { get; set; } 
    public DateTime CommentTime { get; set; } 
    public String CommentBody { get; set; } 
} 

這裏是我的看法(HTML)

<form class="form-stacked" id="comment-form" action="@Url.Action("Index", "Article")" method="post" enctype="multipart/form-data"> 
    <label class="control-label" for="commentTextArea">Leave a comment...</label> 
    <textarea rows="3" id="commentTextArea" name="commentBody" class="span8"></textarea> 
       <input type="submit" value="Post Comment" /> 
</form> 

這是URL

http://localhost:58856/Article?Id=1 

,這是錯誤消息

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int64' for method 'System.Web.Mvc.ActionResult Index(Int64, TechCells.Services.ViewModels.CommentViewModel)' in 'TechCells.Web.UI.Controllers.ArticleController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. 
Parameter name: parameters 

我想MVC會自動檢測從URL id,並自動分配基於名字的ViewModel屬性。對?

這個錯誤的原因是什麼?我該如何解決?

謝謝。

+0

你的路由註冊碼是什麼樣的? – Jasen

+0

@Jasen,它只有默認的路由註冊。 routes.MapRoute( 名: 「TcMainRoute」, URL: 「{控制器}/{行動}」, 的默認值:新的{ 控制器= 「主頁」, 行動= 「索引」, }); – Zafar

回答

2

您的表單沒有 <input name="id" />(這是您的動作簽名所必需的)。請記住,MVC的動作參數是 傳入值,所以如果這是一個表單提交,則需要提供所有這些參數(儘管有時它們可​​以在路由定義中默認,但我不會理解)。

誤讀並錯過了來自GET參數的Id。假設您使用GET & POST操作來執行傳統表單處理,那麼您將該ID傳遞給GET,它呈現一個視圖,然後在POST上該Id被丟棄。所以,有兩個選項:

  • 如果你不需要它,刪除long id參數或使其可選(line id = 0),這樣MVC可進行具有缺省值。
  • 如果你確實需要它,請務必在您的Url.Action()通過它作爲一個routeValue所以它收到回。

此外,不要害怕使用HTML助手,如Html.BeginForm,Html.LabelFor,Html.TextBoxFor

@using (Html.BeginForm("Index", "Article", 
    new { id = __ID_FROM_GET_REQUEST_ }, // to hand-off to POST action 
    FormMethod.Post, 
    new { enctype = "multipart/form-data" } 
)) 
{ 
    @* I assume these are here to help reference what the comment is regarding *@ 
    @Html.HiddenFor(x => x.UserId) 
    @Html.HiddenFor(x => x.ArticleId) 

    @* are these ones auto-generated at creation time? if so, remove these. I 
     don't know enough about your work-flow, so just going to place them for 
     now. I also don't know what your validation requirements are. *@ 
    @Html.HiddenFor(x => x.CommentId) 
    @Html.HiddenFor(x => x.CommentTime) 

    @* Now we get in to user-interaction *@ 
    @Html.LabelFor(x => x.CommentBody) 
    @Html.TextAreaFor(x => x.CommentBody, new { @class = "span8", rows = "3" }) 

    <input type="submit" value="Post Comment" /> 
} 

那麼對於LabelFor工作,確保你裝飾你的模型:

/* ...snip ... **/ 

[Display(Name = "Leave a comment...")] 
public String CommentBody { get; set; } 

/* ...snip ... **/ 
+0

您的意思是:long id = 0?那麼可以爲空? – OzrenTkalcecKrznaric

+0

@OzrenTkalčecKrznarić:或者,儘管我認爲'長?'會更合適(因爲它更簡潔地標記缺失值)。 –

+0

我需要id,從查詢字符串中獲取當前文章ID。但MVC自動識別查詢字符串中的id,對吧?看看這個URL,有id = 1。爲什麼它不會自動獲取ID? – Zafar

0

將其更改爲這個

public ActionResult Index(long id = 0, CommentViewModel comment) 

OR

public ActionResult Index(long? id, CommentViewModel comment) 

將解決問題。

該錯誤發生是因爲您沒有爲id傳遞任何值。並且id不可爲空。 要解決此問題,請通過id或如上所述更改方法簽名。

+0

id不能爲null,不能爲0.它應該有一些有效的值。 – Zafar

+0

在這種情況下,您可以使用任何輸入控件(如下拉列表或隱藏字段)在代碼中確定'id'或從視圖中發送'id'。 –

+0

不是一個好主意... – Zafar