我的Core razor页面模型页面不断抛出异常,并且不会呈现

时间:2019-03-27 18:59:26

标签: c# razor

只需尝试使用pagemodel构建一个简单的剃​​须刀页面即可。不管我做什么,都会引发异常。在编辑模式下,似乎可以看到,但是当 尝试运行它,模型为空。我已经尝试了代码中断,并与其他页面,教程等进行了比较。无论我得到了什么:

  

“ viewdata null”异常

页面

@page
@model TransitSection15.Areas.GTFS.Views.Home.FileSet_NewModel.InputModel
@{
}

<h4>Create a GTFS File Set</h4>
<h1>Add ddd</h1>
@if (ViewBag.Data == null)
{
    @Html.Raw("No data!")
}

@if  (String.IsNullOrEmpty(@Model.Name))
{
string what = "what";
}

<div class="row">
<div class="col-md-4">
    <form method="post">
        <h4>Create a new File Set</h4>
        <div asp-validation-summary="All" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="Name" class="control-label"></label>

代码

public class FileSet_NewModel : PageModel
{
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly ILogger<FileSet_NewModel> _logger;

    public FileSet_NewModel(
            UserManager<ApplicationUser> userManager,
            ILogger<FileSet_NewModel> logger)
    {
        _userManager = userManager;
        _logger = logger;
    }


    [BindProperty]
    public InputModel Input { get; set; }

    public string ReturnUrl { get; set; }

    public string MyHeader { get; set; }

    public class InputModel 
    {
        [Required]
        //[StringLength(120, ErrorMessage = "Name cannot be longer than 
     120 characters.")]
        [Display(Name = "Name")]
        public string Name { get; set; }

        [Required]
        [StringLength(512, ErrorMessage = "Descriptione cannot be longer 
  than 512 characters.")]
        [Display(Name = "Description")]
        public string Description { get; set; } = "Description";

        [Required]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", 
             ApplyFormatInEditMode = true)]
        public DateTime CalendarStartDate { get; set; }

        [Required]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", 
         ApplyFormatInEditMode = true)]
        public DateTime CalendarEndDate { get; set; }
    }


    public void OnGet()
    {
        // ReturnUrl = returnUrl;
         ReturnUrl = "~/index";
    }

    public async Task<IActionResult> OnPostAsync(string returnUrl = null)
    {
        returnUrl = returnUrl ?? Url.Content("~/");
        if (ModelState.IsValid)
        {
            if (String.IsNullOrEmpty(Input.Name))
                Input.Name = String.Empty;

            if (String.IsNullOrEmpty(Input.Description))
                Input.Description = String.Empty;

            if (Input.CalendarStartDate == null )
                Input.CalendarStartDate = DateTime.MinValue;

            if (Input.CalendarEndDate == null)
                Input.CalendarEndDate = DateTime.MaxValue;

            var NewSet = new GTFSFileSet
            {
                Name = Input.Name,
                Description = Input.Description,
                CalendarStartDate = Input.CalendarStartDate,
                CalendarEndDate = Input.CalendarEndDate,
                Active = true,
                SetValid = false,
                CreatedBy = "Steve",
                CreationDate = DateTime.Now        
            };



            /////////////////////////////////
            //do the Save Here
            //
            //GTFSFileSet.Add(NewSet);
          //  await _db.SaveChangesAsync();
          //  return RedirectToPage("/Index");

            var result = false;
            //if (result.Succeeded)
            if (result)
            {
                _logger.LogInformation("User created a new FileSet.");


                //var callbackUrl = Url.Page(
                //    "/Account/ConfirmEmail",
                //    pageHandler: null,
                //    values: new { userId = user.Id, code = code },
                //    protocol: Request.Scheme);


                return LocalRedirect(returnUrl);
            }
            //foreach (var error in result.Errors)
            //{
            //    ModelState.AddModelError(string.Empty, 
       error.Description);
            //}
        }

        // If we got this far, something failed, redisplay form
        return Page();
    }
}

0 个答案:

没有答案