无论我提交表单,Comment对象只有一个值Comment Object
但页面看起来像这样
Create Form
为什么ViewModel值没有正确发布?
public class CommentViewModel{
public Comment comment { get; set; }
public UploadFile Uploadfile { get; set; }
}
这是评论类:
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; }
}
这是视图:
@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" />
}
这是控制器:
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);
}
答案 0 :(得分:1)
您是否尝试过输入类型的HTML帮助程序类,例如:
@Html.Textboxfor(m=>m.comment.CreateDate, new { @class="",@type="datetime",@value="@DateTime.Now.ToString()"})
答案 1 :(得分:0)
对于Userid,CreateDate和FileID尝试使用编辑器。您可以应用自定义类by looking at this question
答案 2 :(得分:-2)
请更新您的观看代码....
@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" />