Reputation: 1714
Total newbie in ASP.Net MVC and Razor Pages.
I have a Razor View with the model referenced declared on top:
@model TestApplication.Models.Registration
How would I be able to debug the model? I've tried setting breakpoints in the model but when debugging, the breakpoints are not being hit.
Code is as follows:
Register.cshtml
@model TestApplication.Models.Registration
@{
string labelClass = "ctrl-label col-sm-4",
controlSize = "col-sm-8";
}
<div class="row">
<div class="col-md-7 col-md-offset-2">
<h2>@TestApplication.Resources.General.Register</h2>
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new{role = "form", @class = "form-horizontal" }))
{
<div class="form-group">
<h3 class="@labelClass">
<small>@TestApplication.Resources.General.CreateAccount</small></h3>
</div>
<hr />
<div class="form-group @Html.ValidationErrorFor(m => m.EmailAddress, "has-error has-feedback")">
@Html.LabelFor(p => p.EmailAddress, new { @class = labelClass })
<div class="@controlSize">
@Html.FormTextBoxFor(p => p.EmailAddress, new { @class = "form-control" })
@if (!Html.IsValid(m => m.EmailAddress))
{
<span class="glyphicon glyphicon-remove form-control-feedback"></span>
}
<span class="hint">@TestApplication.Resources.Forms.RegisterHintEmailAddress</span>
@Html.ValidationMessageFor(m => m.EmailAddress, null, new { @class = "help-block" })
</div>
</div>
<div class="form-group @Html.ValidationErrorFor(m => m.Username, "has-error has-feedback")">
@Html.LabelFor(p => p.Username, new { @class = labelClass })
<div class="@controlSize">
@Html.FormTextBoxFor(p => p.Username, new { @class = "form-control" })
@if (!Html.IsValid(m => m.Username))
{
<span class="glyphicon glyphicon-remove form-control-feedback"></span>
}
@Html.ValidationMessageFor(m => m.Username, null, new { @class = "help-block" })
</div>
</div>
<div class="form-group">
<label class="@labelClass">@TestApplication.Resources.Forms.RegisterLabelStartService</label>
<div class="@controlSize">
@* I AM GETTING AN ERROR ON THIS LINE... *@
@*@foreach(var m in Model.Services)
{
<div class="radio">
<label>
@Html.RadioButtonFor(p => p.StartServiceId, m.Id)
@m.DisplayName
</label>
</div>
}*@
</div>
</div>
}
</div>
</div>
Registration.cs
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Globalization;
using System.ComponentModel.DataAnnotations;
using DataAnnotationsExtensions;
using TestApplication.Resources;
namespace TestApplication.Models
{
public class Registration
{
[Email(ErrorMessageResourceName = "InvalidEmail", ErrorMessageResourceType = typeof(ErrorMessages))]
[Required(ErrorMessageResourceName = "RequiredEmailAddress", ErrorMessageResourceType = typeof(ErrorMessages))]
[HtmlAttribute("placeholder", "PlaceholderEmailAddress", ResourceType = typeof(Forms))]
[Display(Name = "RegisterLabelEmailAddress", ResourceType = typeof(Forms))]
public string EmailAddress { get; set; }
[Email(ErrorMessageResourceName = "InvalidUsername", ErrorMessageResourceType = typeof(ErrorMessages))]
[Required(ErrorMessageResourceName = "RequiredUsername", ErrorMessageResourceType = typeof(ErrorMessages))]
[HtmlAttribute("placeholder", "PlaceholderUsername", ResourceType = typeof(Forms))]
[Display(Name = "RegisterLabelUsername", ResourceType = typeof(Forms))]
[CustomValidation(typeof(Registration), "CheckIfUserExists")]
public string Username { get; set; }
[Display(Name = "RegisterLabelStartService", ResourceType = typeof(Forms))]
public int StartServiceId { get; set; }
public ReadOnlyCollection<ServicePlugin> Services { get; private set; }
public Registration()
{
this.Services = new ReadOnlyCollection<ServicePlugin>(new List<ServicePlugin> { new ServicePlugin { Id = 1, DisplayName = "Mobile Services" }, new ServicePlugin { Id = 2, DisplayName = "Cable Services" } });
}
}
}
ServicePlugin.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TestApplication.Models
{
public class ServicePlugin
{
public int Id { get; set; }
public string DisplayName { get; set; }
}
}
AccountController.cs
[AllowAnonymous]
public ActionResult Register()
{
return this.View();
}
I commented out part of the razor view (the one with ERROR) as I am not able to properly debug the class tied to this.
Specifically, I want to debug this line in Registration.cs file:
**public Registration()**
to figure out how it is being populated and being used in the view.
Appreciate any insight on doing this.
P.S. When I remove the comment on the Register.cshtml where error is happening, I am getting the error: Object Reference not set to an instance of the object.
I can set a breakpoint in this line:
@foreach(var m in Model.Services)
but Model is null and throws the error:
An exception of type 'System.NullReferenceException' occurred in appXXXX.dll but was not handled in user code.
So pretty much I think I need to understand how all of these tie up together.
Upvotes: 0
Views: 708
Reputation: 2316
In your Account controller you need to instantiate and then pass the Model to the view, using an overload of the View
method:
[AllowAnonymous]
public ActionResult Register()
{
return View(new Models.Registration());
}
Upvotes: 1