微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MVC:如果没有@ Html.EditorFor,创建将不起作用

如何解决MVC:如果没有@ Html.EditorFor,创建将不起作用

我目前正在尝试通过创建一个小项目来学习有关MVC的知识。我已经设法完成了许多工作,但是现在我面临着我无法解释的事情。

基本上,我具有通常的设置(模型,查看器,控制器),并且模型数据库显示在视图中。从可以在Visual Studio自动生成的标准Create-View和-Action开始,我打算更改此方法,以便某些单元格具有计算值,而不是来自用户输入。

这对我的 Ready 列和 Ready-Date 列都适用,但是对 Month 列却无效。以某种方式生成新行

@ Html.EditorFor(model => model.Month)

是必需的。如果删除了它(就像我对“就绪”和“就绪日期”所做的那样),则单击“提交”按钮不会创建任何内容,并且该应用程序将保留在“创建页面”上。

我在ModelController中的两个ActionResult看起来像这样:

public ActionResult Create()
{
    //I use this in order to print the UpcomingMonth on the Create-Page
    ViewBag.upcomingMonth = getUpcomingMonth();
    return View();
}

public ActionResult Create([Bind(Include = "Report_ID,Month,Ready,Ready_Date")] Report report)
{
    if (ModelState.IsValid)
    {
        //I would like to use this calculation for the Month-Value
        report.Month = getUpcomingMonth();

        //For Ready and Ready_Date it works to set the values here before adding to the db
        report.Ready = true;
        report.Ready_Date = System.DateTime.Now;

        db.Reports.Add(report);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(report);
}

以及我的视图的相关部分:

<p style="float:right">
    @*Button to create new month*@
    @Html.ActionLink("Create","Create")
</p>

<h2>Latest</h2>

<table class="table">
    <thead>
        <tr>
            @*Column-header: Month*@
            <th style="width: 10%; text-align:center">
                @Html.displayNameFor(model => model.Month)
            </th>

            @*Column-header: True/False is ready*@
            <th style="width: 10%; text-align:center">
                @Html.displayNameFor(model => model.Ready)
            </th>

            @*Column-header: Set ready Date*@
            <th style="width: 20%; text-align:center">
                @Html.displayNameFor(model => model.Ready_Date)
            </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            @*Cell: Latest Month*@
            <td style="width: 10%; text-align:center">
                @Html.displayFor(modelItem => Model.ElementAt(0).Month)
            </td>

            @*Cell: Latest True/False is ready
            <td style="width: 10%; text-align:center">
                ...
            </td>

            @*Cell: Latest Set ready Date*@
            <td style="width: 20%; text-align:center">
                @Html.displayFor(modelItem => Model.ElementAt(0).Ready_Date)
            </td>
            
        </tr>
    </tbody>    
</table>

我真的很感谢您的帮助!

PS:通过ValidationSummary,我得到以下信息(无错误消息):

enter image description here

解决方法

我认为您的视图模型无效。如果将这些行添加到视图中,您会看到什么错误。

@Html.ValidationSummary(true,"Errors:",new { @class = "text-danger bg-danger" })
,

我终于找到了导致这种行为的原因。

在我的模型设置中,我定义了一个验证要求,该要求不允许使用空字符串。因为我在检查ModelState.IsValid之后设置了数据,所以空字符串未通过此检查,因此未添加该行。

我通过取消本月的验证要求解决了该问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。