MVC脚手架中始终需要日期字段

时间:2014-08-12 05:50:52

标签: asp.net-mvc-4 asp.net-mvc-scaffolding

我有一个模型,我用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);
    }

由于

1 个答案:

答案 0 :(得分:2)

只需将Datetime的属性更正为可为空即可。

示例: -

 public DateTime? PlannedStartDate { get; set; }