0
ASP.Net MVC和Razor Pages中的新手。Razor View引用的调试模型
我有一个Razor视图与参考模型中声明之上:
@model TestApplication.Models.Registration
我怎么能调试模式?我试着在模型中设置断点,但是在调试时,断点没有被击中。
代码如下:
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();
}
我注释掉Razor视图(一个有误差),因为我不能够正确地调试依赖于这个类的一部分。
具体来说,我想调试此行Registration.cs文件:
**public Registration()**
要弄清楚它是如何被填充和所使用的视图。
欣赏这样做的任何见解。
P.S.当我删除发生错误的Register.cshtml的注释时,出现错误: 对象引用未设置为对象的实例。
我能在这一行设置一个断点:
@foreach(var m in Model.Services)
但型号是空和引发错误:
型“System.NullReferenceException”的异常出现在appXXXX.dll但没有在用户代码中处理。
所以我觉得我需要了解所有这些如何结合在一起。