0

I want to make a form that user have to fill out it. But I'm getting an error.

Model:

public UserReport Input;

[Key]
//[Required]
public string ReportID { get; set; }


[Required]
public string Description { get; set; }

[Required]
[DataType(DataType.DateTime)]
public string Date { get; set; }

Controller:

private ApplicationDbContext _userReport;
public UserReport Input;

public PageController(ApplicationDbContext userReport)
{
    _userReport = userReport;
}

[HttpGet]
public IActionResult SendReport()
{
    return View();
}

[Authorize]
[HttpPost]
public async Task<IActionResult> SendReport(UserReport userReport)
{
    var user = new UserReport { Description = Input.Description, Date = Input.Date };
    var r = await _userReport.AddAsync(user);
}

View:

@model UserReport
<div class="col-md-6">
    <form id = "SendReport" method="post">
        <div asp-validation-summary="All" class="text-danger"></div>

        <div class="form-group">
            <label asp-for="Input.Description"></label>
            <input asp-for="Input.Description" class="form-control" />
            <span asp-validation-for="Input.Description" class="text-danger"></span>
        </div>

        <div class="form-group">
            <label asp-for="Input.Date"></label>
            <input asp-for="Input.Date" class="form-control" />
            <span asp-validation-for="Input.Date" class="text-danger"></span>
        </div>


        <button type = "submit" class="btn btn-primary">send report</button>
    </form>
</div>

what is wrong with that? It shows me this Error :System.NullReferenceException - Object reference not set to an instance of an object... who can help me to manage this error. Show me where is my mistake please

Mohadese99
  • 51
  • 7
  • 1
    Can you add on which line of the controller you posted you get the error? – Davey van Tilburg Apr 22 '20 at 07:53
  • on this line: var user = new UserReport { Description = Input.Description, Date = Input.Date;} Did i use correct method? – Mohadese99 Apr 22 '20 at 09:26
  • `public UserReport Input;` - what's this for, in either class? Your NullReferenceException is because this is null. I think you need to just use the members of the UserReport class without the `Input.`, e.g. in the SendReport method you want `userReport.Description` etc. not `Input.` – Rup Apr 22 '20 at 11:03
  • If I don't use Input then It won't accepted my View.then I put Input in both class.Do you have any suggestion for that? – Mohadese99 Apr 22 '20 at 17:18

2 Answers2

3

I think you need to go back through the tutorials in the docs again. It doesn't seem like you really know what you're doing here. You've got a field named Input, which seems to be pulled from a Razor Page, but you're working in an MVC controller here. However, even that is off, because you'd typically see that as something like:

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

In a Razor Page. Here, it's not even a property, so even if this would normally do something, it wouldn't as a field, regardless.

The NullReferenceException comes because you reference this field, but never actually initialize it. Again, in a Razor Page (and if it was a property rather than a field), this would get filled with the post data (via BindProperty), but it doesn't work that way in a controller.

In your controller action, you've got a userReport param, so that is where the post data will go. However, since all the asp-for attributes in your view reference Input.Something, nothing will actually get bound to this param. This too seems to be pulled from a Razor Page, without considering that it only works this way in a Razor Page.

Long and short, it looks like you just copied code from other places, without actually understanding any of it or what it does, and cobbled it together into a controller and view. The whole thing is fundamentally broken top-down.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
0

You have mixed with MVC and Razor Pages.

For MVC:

1.Model:

public class UserReport
{
    [Key]
    //[Required]
    public string ReportID { get; set; }

    [Required]
    public string Description { get; set; }

    [Required]
    [DataType(DataType.DateTime)]
    public string Date { get; set; }
}

2.SendReport.cshtml:

@model UserReport
<div class="col-md-6">
    <form asp-action="SendReport" method="post">
        <div asp-validation-summary="All" class="text-danger"></div>

        <div class="form-group">
            <label asp-for="Description"></label>
            <input asp-for="Description" class="form-control" />
            <span asp-validation-for="Description" class="text-danger"></span>
        </div>

        <div class="form-group">
            <label asp-for="Date"></label>
            <input asp-for="Date" class="form-control" />
            <span asp-validation-for="Date" class="text-danger"></span>
        </div>


        <button type="submit" class="btn btn-primary">send report</button>
    </form>
</div>

3.Controller:

public class UserReportsController : Controller
{
    private readonly YourDbContext _context;

    public UserReportsController(YourDbContext context)
    {
        _context = context;
    }
    public IActionResult SendReport()
    {
        return View();
    }

    [HttpPost]
    public async Task<IActionResult> SendReport([Bind("ReportID,Description,Date")] UserReport userReport)
    {
        if (ModelState.IsValid)
        {
            _context.Add(userReport);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(userReport);
    }
}

For Razor Pages:

1.Model:

public class UserReport
{
    [Key]
    //[Required]
    public string ReportID { get; set; }


    [Required]
    public string Description { get; set; }

    [Required]
    [DataType(DataType.DateTime)]
    public string Date { get; set; }
}

2.SendReport.cshtml:

@page
@model SendReportModel

<div class="col-md-6">
    <form id="SendReport" method="post">
        <div asp-validation-summary="All" class="text-danger"></div>

        <div class="form-group">
            <label asp-for="Input.Description"></label>
            <input asp-for="Input.Description" class="form-control" />
            <span asp-validation-for="Input.Description" class="text-danger"></span>
        </div>

        <div class="form-group">
            <label asp-for="Input.Date"></label>
            <input asp-for="Input.Date" class="form-control" />
            <span asp-validation-for="Input.Date" class="text-danger"></span>
        </div>


        <button type="submit" class="btn btn-primary">send report</button>
    </form>
</div>

3.SendReport.cshtml.cs:

public class SendReportModel : PageModel
{
    private readonly YourDbContext _context;

    public SendReportModel(YourDbContext context)
    {
        _context = context;
    }

    public IActionResult OnGet()
    {
        return Page();
    }

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

    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        _context.UserReport.Add(Input);
        await _context.SaveChangesAsync();

        return RedirectToPage("./Index");
    }
}

Reference:

Rena
  • 30,832
  • 6
  • 37
  • 72