在C#中创建错误

时间:2016-07-09 10:55:56

标签: javascript c# asp.net-mvc database razor

我是一名学生,还是C#编程的新手。目前,我正在使用MVC进行一个项目,它有一个计划页面。我需要创建一个计划,但这里是错误的来源。我有一个日期选择器功能,如果我在创建之日起4天内选择任何东西我可以创建但是如果我选择其他时间我就无法创建。

错误:值不能为空。参数:来源

我的时间表创建页面

@model Sec.Models.Schedule
@using Sec.Models
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_LayoutPage.cshtml";
Employee currentEmp = null;
if (ViewBag.emp != null)
{
    currentEmp = (Employee)ViewBag.emp;
}
//Employee List
List<Employee> dbsEmployee = ViewBag.Employee;
List<SelectListItem> lstEmployee =
    (from b in dbsEmployee
     orderby b.Id
     select new SelectListItem()
     { Text = b.Name, Value = b.Id.ToString() }).ToList<SelectListItem>();
lstEmployee.Insert(0, new SelectListItem() { Text = "-- Select --", Value = "" });
//Site List
List<Site> dbsSite = ViewBag.Site;
List<SelectListItem> lstsite =
    (from b in dbsSite
     orderby b.Id
     select new SelectListItem()
     { Text = b.Id.ToString() + " -- " + b.Pod + b.Level + b.Gender, Value = b.Id.ToString() }).ToList<SelectListItem>();
lstsite.Insert(0, new SelectListItem() { Text = "-- Select --", Value = "" });

}
@if ((currentEmp != null) && (currentEmp.isAdmin == true))
{
<script>
    $('#datetimepicker1').data("DateTimePicker").FUNCTION()

</script>

<h2>Create</h2>

using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Schedule</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Employee_Id, "Employee_Id", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("Employee_Id", lstEmployee, htmlAttributes: new { @id = "DrpEmployeeId", @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Employee_Id, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Site_Id, "Site_Id", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("Site_Id", lstsite, htmlAttributes: new { @id = "DrpSiteId", @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Site_Id, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="row">
                <div class='col-sm-6'>
                    <div class='input-group date' id='datetimepicker1'>
                        @Html.TextBoxFor(model => model.Date, new { @class = "form-control datepicker", placeholder = "Press the button to enter date and time" })
                        @Html.ValidationMessageFor(model => model.Date)
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"></span>
                        </span>
                    </div>
                </div>
                <script type="text/javascript">
                    $(function () {
                        $('#datetimepicker1').datetimepicker();
                    });
                </script>
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
}

我的后端

using (secDBEntities3 dbc = new secDBEntities3())
                {
                    DbSet<Employee> dbe = dbc.Employees;
                    ViewBag.Employee = dbe.ToList();
                    DbSet<Site> dbs = dbc.Sites;
                    ViewBag.Site = dbs.ToList();
                }
                return View();

HTTP POST

 public ActionResult Create([Bind(Include = "Id,Site_Id,Employee_Id,Date")] Schedule schedule)
    {
        if (ModelState.IsValid)
        {
            var s = db.Schedules.Create();
            s.Id = schedule.Id;
            s.Site_Id = schedule.Site_Id;
            s.Employee_Id = schedule.Employee_Id;
            s.Date = schedule.Date;
            db.Schedules.Add(schedule);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(schedule);
    }

堆栈跟踪

[ArgumentNullException: Value cannot be null.
Parameter name: source]
   System.Linq.OrderedEnumerable`2..ctor(IEnumerable`1 source, Func`2 keySelector, IComparer`1 comparer, Boolean descending) +4367342
   System.Linq.Enumerable.OrderBy(IEnumerable`1 source, Func`2 keySelector) +62
   ASP._Page_Views_Schedules_Create_cshtml.Execute() in D:\Jncr\Jncrwa\Jncrwa\Views\Schedules\Create.cshtml:13
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +198
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +173
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9744261
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
  

我已搜索此错误,但找不到解决问题的方法。

1 个答案:

答案 0 :(得分:0)

代码实际上没有错误。这个错误是关于日期时间选择器,因为我实际上正在测试服务器。为了解决这个问题,我们设置了一个try-catch来确保它不会使系统崩溃。