-1

whatever I submit the form ,the Comment object just have one value Comment Object but the pages look like this Create Form
Why aren't the ViewModel values posting correctly?

public class CommentViewModel{

    public Comment comment { get; set; }
    public UploadFile Uploadfile { get; set; }
}

This is Comment class:

public class Comment{

    [Key]
    public int Id { get; set; }
    public int FileId { get; set; }
    public DateTime CreateDate { get; set; }
    public string review { get; set; }
    public string UserId { get; set; }
}

This is View:

@using (Ajax.BeginForm(ajaxopts)){

    @Html.AntiForgeryToken()
    <h4>Comment</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    @Html.LabelFor(model => model.comment.FileId, htmlAttributes: new { @class = "control-label col-md-2" })

    <input class="form-control text-box single-line" data-val="true" data-val-number="The field FileId must be a number." data-val-required="FileId is necessary" id="FileId" name="FileId" type="number" value="@ViewBag.FileId"  />

    @Html.LabelFor(model => model.comment.CreateDate, htmlAttributes: new { @class = "control-label col-md-2" })

   <input class="form-control text-box single-line" data-val="true" data-val-date="The field CreateDate must be a date." data-val-required="CreateDate is necessary" id="CreateDate" name="CreateDate" type="datetime" value="@DateTime.Now.ToString()" />

   @Html.LabelFor(model => model.comment.UserId, htmlAttributes: new { @class = "control-label col-md-2" })

  <input class="form-control text-box single-line" id="UserId" name="UserId" type="text" value="@User.Identity.Name.ToString()"  />

   @Html.ValidationMessageFor(model => model.comment.UserId, "", new { @class = "text-danger" })

   @Html.LabelFor(model => model.comment.review, htmlAttributes: new { @class = "control-label col-md-2" })

   @Html.EditorFor(model => model.comment.review, new { htmlAttributes = new { @class = "form-control" } })

   <input type="submit" value="Create" class="btn btn-default" /> 
}

This is Controller:

  public ActionResult CreateCommets(Comment comment){

        if (ModelState.IsValid)
        {
            db.Comments.Add(comment);
            db.SaveChanges();
        }
        List<Comment> Commentlist = (from p in db.Comments where p.FileId == comment.FileId select p).ToList();
        return PartialView("_CommentsView",Commentlist);
    }
Minh Bui
  • 1,032
  • 8
  • 18
CBBing
  • 9
  • 4
  • is there a [HttpPost] attribute on the CreateComments Function? – Dawood Awan May 17 '16 at 06:53
  • yeah,I add it before I create the question – CBBing May 17 '16 at 07:00
  • can you explain 'ajaxopts' ? – Usman May 17 '16 at 07:02
  • AjaxOptions ajaxopts = new AjaxOptions { HttpMethod = "post", UpdateTargetId = "comments", Url = Url.Action("CreateCommets", "UploadFiles"), LoadingElementId = "loading", LoadingElementDuration = 2000 }; – CBBing May 17 '16 at 07:05
  • use CommentViewModel in your action method parameter. public ActionResult CreateCommets(CommentViewModel commentViewModel){//code} – Karthik M R May 17 '16 at 07:15
  • but I post Comment – CBBing May 17 '16 at 07:18
  • why not just use `html.beginform` ? any particular reason for using ajax? – Usman May 17 '16 at 07:22
  • I want to use ajax to update commets but the Web page don't change – CBBing May 17 '16 at 07:32
  • try using `EditorFor` instead of `TextBoxFor` – Usman May 17 '16 at 07:47
  • I used Editfor before ,but useless – CBBing May 17 '16 at 13:29
  • Because the model in your view is `CommentViewModel` which means the parameter in your method needs to be `public ActionResult CreateCommets(CommentViewModel model) {` (not `Comment comment`). Or you could use `public ActionResult CreateCommets([Bind(Prefix = "comment")]Comment model)` –  May 19 '16 at 00:52
  • But your not understanding what a view model is. It should not contain properties which are data model. It should contain one property for each property of `Comment` that you want to display/edit in the view plus the `Uploadfile` file property. –  May 19 '16 at 00:57
  • I don't think so, because I find the reason is I shouldn't use ' ‘ instead of 'Html.Editor()' – CBBing May 20 '16 at 13:47

3 Answers3

1

Have you tried HTML helper class for Input type eg:

 @Html.Textboxfor(m=>m.comment.CreateDate, new  { @class="",@type="datetime",@value="@DateTime.Now.ToString()"})
Aniket Sharma
  • 1,000
  • 10
  • 16
0

For Userid, CreateDate and FileID try and use an Editor for. You can apply your custom class by looking at this question

Community
  • 1
  • 1
JordanW
  • 121
  • 1
  • 3
-2

Please update your view code....

 @model ApplicationName.Models.Comment

 <h4>Comment</h4>
 <hr />
 @Html.ValidationSummary(true, "", new { @class = "text-danger" })

 @Html.LabelFor(model => model.FileId, htmlAttributes: new { @class = "control-label col-md-2" })

 <input class="form-control text-box single-line" data-val="true" data-val-number="The field FileId must be a number." data-val-required="FileId is necessary" id="FileId" name="FileId" type="number" value="@ViewBag.FileId" />

 @Html.LabelFor(model => model.CreateDate, htmlAttributes: new { @class = "control-label col-md-2" })

 <input class="form-control text-box single-line" data-val="true" data-val-date="The field CreateDate must be a date." data-val-required="CreateDate is necessary" id="CreateDate" name="CreateDate" type="datetime" value="@DateTime.Now.ToString()" />

  @Html.LabelFor(model => model.UserId, htmlAttributes: new { @class = "control-label col-md-2" })

  <input class="form-control text-box single-line" id="UserId" name="UserId" type="text" value="@User.Identity.Name.ToString()" />

 @Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })

 @Html.LabelFor(model => model.review, htmlAttributes: new { @class = "control-label col-md-2" })

 @Html.EditorFor(model => model.review, new { htmlAttributes = new { @class = "form-control" } })

 <input type="submit" value="Create" class="btn btn-default" /> 
Banwari Yadav
  • 506
  • 1
  • 3
  • 18
  • You can directly access your model like this way.. @Html.LabelFor(model => model.UserId) Or if you are working with the mvc pattren then you have to use @Html.TextBoxFor(model=>model.CreateDate, new {@class="", @type="datetime", @value="@DateTimeNow.ToString"}) – Banwari Yadav May 17 '16 at 06:32
  • If you have any problem with this please let me know, i will provide you better explaination. – Banwari Yadav May 17 '16 at 06:39
  • I use @model MVCdemo.Models.CommentViewModel becasue they are two models – CBBing May 17 '16 at 06:41
  • if you are using two model..why not you are using partial view? – Banwari Yadav May 17 '16 at 06:49
  • yeah,I use some partial view – CBBing May 17 '16 at 06:52
  • I can't understand you, please provide your full code, how you are using – Banwari Yadav May 17 '16 at 06:56
  • I share it to google https://drive.google.com/open?id=0B6XkUOlmSKWUSERfb0ZFVFFHc0k – CBBing May 17 '16 at 07:26
  • Hi...@CBBing please use the another partial view under the @Html.Partial("_CommentsView") @Html.Partial("_CommentsFormView") Inside this _CommentsFormView you have to use single model Comment and use my above answer view html – Banwari Yadav May 17 '16 at 07:40
  • let me explain what I do.I want to create Comment just like I create Comment now,I don't want this web page Reload and show the Comment – CBBing May 17 '16 at 14:11
  • I use Ajax.BeginForm to post Comment ,but I received "NULL" – CBBing May 17 '16 at 14:13