Thursday 15 July 2010

c# - Cannot resolve ASP.NET MVC error -


I am a new learner of ASP.NET MVC. Businesses should be easy to answer this problem. I am trying to submit a form (post comment), and am getting the error.

Here is my controller

  [HTTP post] Public ActionRashult Index (long id, comment comment modem) {var service = new UserService () ; Var articleService = New ArticleService (); Comment. User ID = service.GetUserByUsername (User.Identity.Name) .UserId; Comment. Article. Id = id; Comment. Comm DateTime = DateTime.Now; ArticleService.AddArticleComment (comment); See Return (); }   

My view is here

  Public category CommentViewModel: BaseViewModel {public Int64 CommentId {get; Set; } Public Int64 UserId {get; Set; } Public Int64 ArticleId {get; Set; } Public Date TimeTime {Received; Set; } Receive public string comment body { Set; }}   

This is my view (HTML)

  & lt; Form class = "form-stacked" id = "comment form" action = "@ Url.Action (" index "," paragraph ")" method = "post" encrypt = "multipart / form-data" & gt; & Lt; = "CommentTextArea" for label class = "control-label" & gt; Leave a comment ... & lt; / Label & gt; & Lt; Texterelines = "3" id = "comment texture" name = "comment body" class = "span 8" & gt; & Lt; / Textarea & gt; & Lt; Input type = "submit" value = "post comment" /> & Lt; / Form & gt;   

This is the URL

  http: // localhost: 58856 / article? ID = 1   

and this is an error message

  The parameter dictionary contains 'non-nullable' type 'System.Int64' The parameter 'id' is a blank entry for 'System.Web.Mvc.ActionResult Index (Int64, TechCells.Services.ViewModels.CommentViewModel)' for TechCells.Web.UI.Controllers.ArticleController '. An optional parameter should be a reference type, a faucet type, or should be declared as an optional parameter. Parameter Name: Parameter   

I think MVC should automatically detect id from URL and automatically show ViewModel properties based on names Should be exchanged. right?

What is the reason for this error? How can I decide?

Thanks

In your form, & lt; Input name = "id" / & gt; (which is required for your action signature). Remember that MVC's action arguments are incoming values ​​, so if it is a form submission, then all those parameters should be supplied (though sometimes they can be omitted in the root definition , But I will not get into it

Reading and erasing the ID coming from the gate parameter. Suppose you follow a GET & traditionally deal with it; Posted Action, you get to the gate It passes a view, then the ID is dropped on the mail, therefore, two options:

  • If you do not need it, then long ID parameter or make it optional ( line id = 0 ) so that MVC can continue to a default value.
  • If you need it, Make sure to pass in the form of a route value in Url.Action () . It has been returned.

    Besides, do not be afraid to use HTML helpers, such as Html.BeginForm , Html.LabelFor , Html.TextBoxFor , etc.

      @using (Html.BeginForm ("index", "paragraph", new {id = __ID_FROM_GET_REQUEST_}, // post-action to hand-off FormMethod.Post, new {enctype = "multipart / Form-data "}) {@ * I think this is what the reference comment is about * @ @ html. HiddenFor (X => x.UserId) @ Html.HiddenFor (x = & gt; x.ArticleId) @ * Are these generations generated automatically on time? If so, remove these. I do not have enough information about your work flow, so just going to give them a place for now I do not even know what your verification requirements are * @@Html.HiddenFor (x = & gt; x.CommentId ) @ Html.HiddenFor (x = & gt; x.CommentTime) @ * We now come in contact with the user * @ @ html.LabelFor (x = & gt; x .commentBody) @ Html.TextAreaFor (x = & gt; ; X.CommentBody, new {@class = "span8", lines = "3"}) & lt; Input type = "submit" value = "post comment" /> }   

    Then to work for LabelFor , make sure that you decorate your model:

      / * ... snip ... ** / [Display (name = "Leave a comment ...")] Public string comment body {Received; Set; } / * ... harvesting ... ** /    

No comments:

Post a Comment