0

我是 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();
}

这是我的视图模型

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>

这是网址

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 应该自动检测id来自 URL 的,并根据名称自动分配 ViewModel 属性。正确的?

这个错误的原因是什么?我该如何解决?

谢谢。

4

2 回答 2

2

您的表单没有<input name="id" />(根据您的操作签名是必需的)。请记住,MVC 的操作参数是传入的值,因此如果这是一个表单提交,则需要提供所有这些参数(尽管有时它们可​​以在路由定义中默认,但我不会介入)。

误读并错过了来自 GET 参数的 Id。假设您使用 GET 和 POST 操作遵循传统的表单处理,您将 Id 传递给 GET,它呈现一个视图,然后在 POST 上删除 Id。所以,有两个选择:

  • 如果您不需要它,请删除该long id参数或将其设为可选 ( line id = 0),以便 MVC 可以继续使用默认值。
  • 如果您确实需要它,请确保将其Url.Action()作为 routeValue 传递给您,以便重新接收。

此外,不要害怕使用 HTML 帮助程序,例如Html.BeginFormHtml.LabelForHtml.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 ... **/
于 2013-06-22T18:11:22.643 回答
0

改成这个

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

或者

这个

public ActionResult Index(long? id, CommentViewModel comment)

将解决问题。

发生错误是因为您没有为id. 并且id不可为空。要解决此问题,请按上述方法传递id或更改方法签名。

于 2013-06-22T18:23:31.380 回答