2014-09-03 82 views
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但沒有在用戶代碼中處理。

所以我覺得我需要了解所有這些如何結合在一起。

回答

1

在您的帳戶控制器,你需要實例,然後將模型傳遞給視圖,使用View方法的重載:

[AllowAnonymous] 
public ActionResult Register() 
{ 
    return View(new Models.Registration()); 
}