我正在创建一个简单的博客。当用户向特定条目添加评论时,发出发布请求时,评论详细信息未绑定?
当我抛出一个断点和post方法时,所有带有“红十字标志”并指出 “隐式函数评估已由用户关闭”的参数 。
这里是所有代码。
控制器
using Blog.Models;
using Blog.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
namespace Blog.Controllers
{
public class CommentsController : Controller
{
private ApplicationDbContext _context;
public CommentsController()
{
_context = new ApplicationDbContext();
}
// GET: Comments
public ActionResult Index(int entryId)
{
Entry entryFromDb = _context.Entries.Include(c => c.Category).Single(e => e.Id == entryId);
EntryAndCommentViewModel viewModel = new EntryAndCommentViewModel()
{
entry = entryFromDb,
EntryId = entryId
};
return View("CommentForm",viewModel);
}
[HttpPost]
public ActionResult Save(Comment comment)
{
Entry entryFromDb = _context.Entries.Include(c => c.Category).Single(e => e.Id == comment.EntryId);
if (!ModelState.IsValid)
{
EntryAndCommentViewModel _viewModel = new EntryAndCommentViewModel()
{
entry = entryFromDb,
EntryId = comment.EntryId
};
return View("CommentForm", _viewModel);
}
return View();
}
}
}
ViewModel
using Blog.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Blog.ViewModels
{
public class EntryAndCommentViewModel
{
[Required]
public int EntryId { get; set; }
public Entry entry { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
[Required]
[StringLength(100)]
[EmailAddress]
public string EmailAddress { get; set; }
[Required]
[StringLength(1000)]
public string CommentDetails { get; set; }
}
}
查看
@model Blog.ViewModels.EntryAndCommentViewModel
@{
ViewBag.Title = "CommentForm";
}
<br />
<p class="alert alert-danger">Add <b>Comment</b> to this Post.</p>
<h3>
<b>
@Model.entry.Subject
</b>
</h3>
<p>
<i>
Category : <b>@Model.entry.Category.Name</b> - Posted on @Model.entry.PostDate.ToString("dd MMM yyyy")
</i>
</p>
<p>
@Model.entry.Body
</p>
<P>
(0) Comments
</P>
<br />
<h2>Leave a Comment</h2>
<br />
@using (Html.BeginForm("Save", "Comments"))
{
@Html.HiddenFor(m => m.entry.Id)
<div class="form-group">
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name, new { @class = "form-control", autofocus = "autofocus", Placeholder = "e.g James Anderson" })
@Html.ValidationMessageFor(m => m.Name)
</div>
<div class="form-group">
@Html.LabelFor(m => m.EmailAddress)
@Html.TextBoxFor(m => m.EmailAddress, new { @class = "form-control", Placeholder = "e.g YourEmail@Domain.com" })
@Html.ValidationMessageFor(m => m.EmailAddress)
</div>
<div class="form-group">
@Html.LabelFor(m => m.CommentDetails)
@Html.TextAreaFor(m => m.CommentDetails, 4, 8, new { @class = "form-control", Placeholder = "e.g Comment description goes here!" })
@Html.ValidationMessageFor(m => m.CommentDetails)
</div>
<button type="submit" class="btn btn-primary">Save</button>
<button type="reset" class="btn btn-link">Reset</button>
}
如何解决?谢谢
答案 0 :(得分:3)
打开或关闭自动属性评估
在“工具”菜单上,单击“选项”。 在“选项”对话框中,打开“调试”节点,然后单击“常规”。 如果未出现“调试”节点,请单击“显示所有设置”。 选择或清除“启用属性评估和其他隐式函数调用”复选框,然后单击“确定”