关于asp.net mvc:Razor View引用的调试模型

Debugging Model Referenced by Razor View

ASP.Net MVC 和 Razor Pages 的全部新手。

我有一个 Razor 视图,上面声明了引用的模型:

1
@model TestApplication.Models.Registration

我怎样才能调试模型?我试过在模型中设置断点,但是在调试时,断点没有被击中。

代码如下:

注册.cshtml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@model TestApplication.Models.Registration
@{
    string labelClass ="ctrl-label col-sm-4",
        controlSize ="col-sm-8";  
}


   
        @TestApplication.Resources.General.Register
        @using (Html.BeginForm("Register","Account", FormMethod.Post, new{role ="form", @class ="form-horizontal" }))
        {
           
                <h3 class="@labelClass">
                    <small>@TestApplication.Resources.General.CreateAccount</small>
           

            <hr />

             m.EmailAddress,"has-error has-feedback")">
                @Html.LabelFor(p => p.EmailAddress, new { @class = labelClass })
               
                    @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" })
               
           

             m.Username,"has-error has-feedback")">
                @Html.LabelFor(p => p.Username, new { @class = labelClass })
               
                    @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" })
               
           

           
                <label class="@labelClass">@TestApplication.Resources.Forms.RegisterLabelStartService</label>
               

                @* I AM GETTING AN ERROR ON THIS LINE... *@
                @*@foreach(var m in Model.Services)
                {
                   
                        <label>
                            @Html.RadioButtonFor(p => p.StartServiceId, m.Id)
                            @m.DisplayName
                        </label>
                   
                }*@
               
           
        }

注册.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

1
2
3
4
5
[AllowAnonymous]
public ActionResult Register()
{
  return this.View();
}

我注释掉了 razor 视图的一部分(带有 ERROR 的视图),因为我无法正确调试与此相关的类。

具体来说,我想在 Registration.cs 文件中调试这一行:

1
**public Registration()**

弄清楚它是如何在视图中填充和使用的。

感谢您对这样做的任何见解。

附言当我删除发生错误的 Register.cshtml 上的评论时,我收到错误消息:
未将对象引用设置为对象的实例。

我可以在这一行设置断点:

1
@foreach(var m in Model.Services)

但模型为空并抛出错误:

"System.NullReferenceException"类型的异常发生在 appXXXX.dll 中,但未在用户代码中处理。

所以我想我几乎需要了解所有这些是如何联系在一起的。


在您的 Account 控制器中,您需要使用 View 方法的重载实例化然后将模型传递给视图:

1
2
3
4
5
[AllowAnonymous]
public ActionResult Register()
{
  return View(new Models.Registration());
}