我正在尝试验证表单。出于某种原因,当我提交表单时,表单验证显示“ID 字段是必需的”。然而,尽管它这么说,我有一个 HiddenFor 输入应该照顾它......
这是我的UserReportForm.cshtml视图(为便于阅读而缩短):
@model BugTracker.ViewModels.UserReportViewModel
@{
ViewBag.Title = "Issue Form";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Report Bug/Request Change</h1>
@Html.ValidationSummary(false, "Please fix the following errors.")
@using (Html.BeginForm("Save", "Report", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
<label>Are you reporting a bug, or requesting a change to a page?</label>
@Html.DropDownListFor(m => m.Report.RequestTypeID, new SelectList(Model.RequestType, "ID", "RequestBC"), "Select Issue", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Report.RequestTypeID)
</div>
<div class="form-group">
<label>Name of Issue</label>
@Html.TextBoxFor(m => m.Report.Name, new { @class = "form-control"})
@Html.ValidationMessageFor(m => m.Report.Name)
</div>
<div class="form-group">
<label>Detailed Description of Issue</label>
@Html.TextAreaFor(m => m.Report.Description, new { @class = "form-control", @rows = "10"})
@Html.ValidationMessageFor(m => m.Report.Description)
</div>
@Html.HiddenFor(m => m.Report.ID)
<button type="submit" class="btn btn-primary">Save</button>
}
这是视图模型UserReportViewModel.c:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BugTracker.Models;
using System.Web.Mvc;
namespace BugTracker.ViewModels
{
public class UserReportViewModel
{
public Report Report { get; set; }
public IEnumerable<RequestType> RequestType { get; set; }
}
}
和Report.cs模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace BugTracker.Models
{
public class Report
{
//For User
public int ID { get; set; }
[Required]
public String Name { get; set; }
[Required]
public String Description { get; set; }
public RequestType RequestType { get; set; }
[Display (Name="Request Type")]
public byte RequestTypeID { get; set; }
}
}
在我添加验证之前,这些表单在数据库中获取 ID 没有问题(每次增加 1)。为什么这会抱怨 ID,我该怎么做才能阻止它说它是必需的?
编辑:我认为这与它没有太大关系,但经过进一步审查,我认为我的控制器可能导致了这个问题:
新表格
public ActionResult UserReportForm()
{
var requestType = _context.RequestType.ToList();
var viewModel = new UserReportViewModel
{
RequestType = requestType,
};
return View(viewModel);
}
保存表格
[HttpPost]
public ActionResult Save(UserReportViewModel viewModel)
{
if (!ModelState.IsValid)
{
var viewM = new UserReportViewModel
{
RequestType = _context.RequestType.ToList(),
};
return View("UserReportForm", viewM);
}
if (viewModel.Report.ID == 0)
{
_context.Reports.Add(viewModel.Report);
}
else
{
var reportInDb = _context.Reports.Single(c => c.ID == viewModel.Report.ID);
reportInDb.Name = viewModel.Report.Name;
reportInDb.Description = viewModel.Report.Description;
reportInDb.RequestTypeID = viewModel.Report.RequestTypeID;
}
_context.SaveChanges();
return RedirectToAction("Report", "Report", new { stat = 1 });
}
感谢您的回复,SAR 和 Tetsuya Yamamoto - 感谢您的建议!在做了一些更多的研究之后,我能够解决我的问题。我发现的解决方案是我如何使用视图模型。
首先,似乎@Html.ValidationSummary 将检查每个字段以确定模型中定义的信息是否是必需的。就我而言,我的报告模型中定义的 ID是必需的。鉴于这种情况,我可以通过简单地从我的视图中删除这条线来解决我的问题:
@Html.HiddenFor(m => m.Report.ID)
但是,我想使用这个视图来创建表单和编辑它。在这种情况下,我们需要让这个 ID 可以为空。解决方案是编辑视图模型 UserReportViewModel.cs,而不是编辑 Report 模型。我打开这个文件,并添加了这行代码:
public int? Id {get; set;}
然后,我又回到我的观点,把它改成了这样:
@Html.HiddenFor(m => m.Id)
快完成了。回到控制器,我需要稍微调整一下。我的新控制器没有变化,但在保存控制器中,我将使用 viewModel.Report.ID 的实例更改为 viewModel.Id。我还在“if (!ModelState.Valid) 分支”中添加了一行代码。更新后的代码如下所示:
[HttpPost]
public ActionResult Save(UserReportViewModel viewModel)
{
if (!ModelState.IsValid)
{
var viewM = new UserReportViewModel
{
RequestType = _context.RequestType.ToList(),
};
viewM.Id = viewModel.Report.ID;
return View("UserReportForm", viewM);
}
if (viewModel.Id == 0)
{
_context.Reports.Add(viewModel.Report);
}
else
{
var reportInDb = _context.Reports.Single(c => c.ID == viewModel.Id);
reportInDb.Name = viewModel.Report.Name;
reportInDb.Description = viewModel.Report.Description;
reportInDb.RequestTypeID = viewModel.Report.RequestTypeID;
}
_context.SaveChanges();
return RedirectToAction("Report", "Report", new { stat = 1 });
}
在这些更改之后,我能够通过验证成功创建和编辑我的表单!总之,解决这个问题的方法是在视图模型中使用可以为空的 ID,而不是来自特定模型的必需 ID。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句