ASP.Net MVC和Razor Pages中的新手。
我有一个Razor View,其中引用的模型在顶部声明:
@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();
}
我注释掉了部分剃刀视图(带有ERROR的视图),因为我无法正确调试与此相关的类。
具体来说,我想在Registration.cs文件中调试这一行:
**public Registration()**
了解如何在视图中填充和使用它。
欣赏有关这样做的任何见解。
P.S。当我删除Register.cshtml上发生错误的注释时,我收到错误: 对象引用未设置为对象的实例。
我可以在这一行设置一个断点:
@foreach(var m in Model.Services)
但是Model为null并抛出错误:
类型&#39; System.NullReferenceException&#39;的例外情况发生在appXXXX.dll中,但未在用户代码中处理。
我认为我需要了解所有这些是如何结合在一起的。
答案 0 :(得分:1)
在您的帐户控制器中,您需要实例化,然后使用View
方法的重载将模型传递给视图:
[AllowAnonymous]
public ActionResult Register()
{
return View(new Models.Registration());
}