【发布时间】:2014-09-03 19:22:50
【问题描述】:
ASP.Net MVC 和 Razor 页面的新手总数。
我有一个 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();
}
我注释掉了剃刀视图的一部分(带有错误的视图),因为我无法正确调试与此相关的类。
具体来说,我想在Registration.cs文件中调试这一行:
**public Registration()**
弄清楚它是如何在视图中填充和使用的。
感谢您对此的任何见解。
附:当我删除发生错误的 Register.cshtml 上的注释时,我收到错误消息: 对象引用未设置为对象的实例。
我可以在这一行设置断点:
@foreach(var m in Model.Services)
但是Model为null并抛出错误:
appXXXX.dll 中出现“System.NullReferenceException”类型的异常,但未在用户代码中处理。
我想我需要了解所有这些是如何联系在一起的。
【问题讨论】:
标签: asp.net-mvc razor