我有一个模型,我用Mvc Scaffolding搭建它,我模型中DateTime Property
的所有字段都标记为Required
我的意思是我不能在其中输入空值。即使在我的模型中,Date字段也没有[Required]
属性。我想摆脱这个问题,任何想法?
这是我的模特。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVCMembershipBootstrap.Models.FirstComponent
{
public class FirstCmActivity
{
public int FirstCmActivityId { get; set; }
public string Name { get; set; }
public DateTime PlannedStartDate { get; set; }
public DateTime PlannedEndDate { get; set; }
public DateTime OngoingStartDate { get; set; }
public DateTime OngoingEndDate { get; set; }
public DateTime FinishedDate { get; set; }
public string GizResponsible { get; set; }
public string PartnerResponsible { get; set; }
public string swAfghanSide { get; set; }
public string swGiz { get; set; }
public string swRodeco { get; set; }
public string swExtern { get; set; }
public string EquipmentNeeds { get; set; }
public string EquipmentExist { get; set; }
public string MileStone { get; set; }
public int FirstCmOutputId { get; set; }
public virtual FirstCmOutput Output { get; set; }
}
}
这是我的控制器...
public class FirstCmActivitiesController : Controller
{
private OPandMEContext context = new OPandMEContext();
//
// GET: /FirstCmActivities/
public ViewResult Index()
{
return View(context.FirstCmActivities.ToList());
}
public ViewResult Report()
{
return View();
}
//
// GET: /FirstCmActivities/Details/5
public ViewResult Details(int id)
{
FirstCmActivity firstcmactivity = context.FirstCmActivities.Single(x => x.FirstCmActivityId == id);
return View(firstcmactivity);
}
//
// GET: /FirstCmActivities/Create
public ActionResult Create()
{
ViewBag.PossibleFirstCmOutputs = context.FirstCmOutputs;
return View();
}
//
// POST: /FirstCmActivities/Create
[HttpPost]
public ActionResult Create(FirstCmActivity firstcmactivity)
{
if (ModelState.IsValid)
{
context.FirstCmActivities.Add(firstcmactivity);
context.SaveChanges();
Response.Redirect("http://localhost:53785/firstcmActivities/Create#services");
}
ViewBag.PossibleFirstCmOutputs = context.FirstCmOutputs;
return View(firstcmactivity);
}
由于
答案 0 :(得分:2)
只需将Datetime的属性更正为可为空即可。
示例: -
public DateTime? PlannedStartDate { get; set; }