简体   繁体   English

隐式Model Binder无法在MVC中工作?

[英]Implicit Model Binder is not working in MVC?

I am creating a simple blog. 我正在创建一个简单的博客。 When the User add a comment to a specific Entry that Comment details is not bind when the post request is issued ? 当用户向特定条目添加评论时,发出发布请求时,评论详细信息未绑定?

And when i throw a breakpoint and the post method All the parameter having a "Red Cross Sign" and stating that "Implicit function evaluation is turned off by the User" . 当我抛出一个断点和post方法时,所有参数都带有一个“红十字标志”并指出“用户关闭了隐函数评估”

Here is all the Code. 这是所有代码。

Controller 调节器

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; }

    }
}

View 视图

@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>

}

How to fix it ? 如何解决? Thanks 谢谢

To turn automatic property evaluation on or off 打开或关闭自动属性评估

On the Tools menu, click Options. 在工具菜单上,单击选项。 In the Options dialog box, open the Debugging node, and click General. 在“选项”对话框中,打开“调试”节点,然后单击“常规”。 If the Debugging node does not appear, click Show all settings. 如果未出现“调试”节点,请单击“显示所有设置”。 Select or clear the Enable property evaluation and other implicit function calls check box, and then click OK 选择或清除“启用属性评估和其他隐式函数调用”复选框,然后单击“确定”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM