调试视图中的代码(asp.net mvc2)

时间:2010-08-23 06:54:21

标签: c# asp.net-mvc visual-studio asp.net-mvc-2

如何在asp.net mvc2应用程序的View中调试代码?

昨晚进度后修改

好的,现在我有以下内容:

Shared\EditorTemplates\Equipment.ascx中的

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DAT.Models.Item>" %>

    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>

        <div class="editor-label">
            <%: Html.Label("Item ID") %>
            <%: Html.TextBoxFor(model => model.ItemID) %>
            <%: Html.ValidationMessageFor(model => model.ItemID) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.ModelID) %>
            <%: Html.DropDownListFor(x => x.Model.Model1, new SelectList(Model.Model.Model1, "ModelId", "Model", Model.ModelID)) %>
            <%: Html.ValidationMessageFor(model => model.ModelID) %>
        </div>

        ...
Item\Edit.aspx中的

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<DAT.ViewModels.ItemEditViewModel>" %>

    <% using (Html.BeginForm()) {%>
            <%: Html.ValidationSummary(true) %>

            <fieldset>
                <legend>Fields</legend>
                    <%: Html.EditorFor(model => model.Item) %>
                <p>
                    <input type="submit" value="Save" />
                </p>
            </fieldset>

控制器:

    public ActionResult Edit(int id)
    {
        var models = from p in database.Models.Where(x => x.Model1 != null) select p;

        var viewModel = new ViewModel
                            {
                                Things = database.Things.Single(a => a.ItemID == id),

                                //tried this:
                                //Models = database.Models                 

                                Models = models
                            };

        return View(viewModel);
    }

所以我确信问题在于这一行

<%: Html.DropDownListFor(x => x.Model.Model1, new SelectList(Model.Model.Model1, "ModelId", "Model", Model.ModelID)) %>

生成选择列表时,我没有第一个参数的IEnumerable?或者我提供的其中一个值导致null。如何在我的视图中获取模型列表?

在拉出我所有的头发后编辑:

问题似乎在于这个例子:http://www.asp.net/mvc/tutorials/mvc-music-store-part-4。奇怪的是,我不确定它是否遵循最佳实践。看一下代码以及它们如何通过模型 - 使用ViewData [“Blah”]和模型似乎很晦涩难懂,为什么你不能把它全部作为模型发送?看看代码是如何完成的:

Album.ascx:

<%@ Import Namespace="MvcMusicStore"%>

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcMusicStore.Models.Album>" %>

<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>

<p>
    <%: Html.LabelFor(model => model.Title)%>
    <%: Html.TextBoxFor(model => model.Title)%>
    <%: Html.ValidationMessageFor(model => model.Title)%>
</p>
<p>
    <%: Html.LabelFor(model => model.Price)%>
    <%: Html.TextBoxFor(model => model.Price)%>
    <%: Html.ValidationMessageFor(model => model.Price)%>
</p>
<p>
    <%: Html.LabelFor(model => model.AlbumArtUrl)%>
    <%: Html.TextBoxFor(model => model.AlbumArtUrl)%>
    <%: Html.ValidationMessageFor(model => model.AlbumArtUrl)%>
</p>
<p>
    <%: Html.LabelFor(model => model.Artist)%>
    <%: Html.DropDownList("ArtistId", new SelectList(ViewData["Artists"] as IEnumerable, "ArtistId", "Name", Model.ArtistId))%>
</p>
<p>
    <%: Html.LabelFor(model => model.Genre)%>
    <%: Html.DropDownList("GenreId", new SelectList(ViewData["Genres"] as IEnumerable, "GenreId", "Name", Model.GenreId))%>
</p>

查看:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcMusicStore.ViewModels.StoreManagerViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Edit - <%: Model.Album.Title %>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Edit Album</h2>

    <% Html.EnableClientValidation(); %>

    <% using (Html.BeginForm()) {%>

    <fieldset>
        <legend>Edit Album</legend>
        <%: Html.EditorFor(model => model.Album, new { Artists = Model.Artists, Genres = Model.Genres}) %>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

    <% } %>

    <div>
        <%:Html.ActionLink("Back to Albums", "Index") %>
    </div>

</asp:Content>

查看型号:

using System.Collections.Generic;
using MvcMusicStore.Models;

namespace MvcMusicStore.ViewModels
{
    public class StoreManagerViewModel
    {
        public Album Album { get; set; }
        public List<Artist> Artists { get; set; }
        public List<Genre> Genres { get; set; }
    }
}

和控制器:

//
// GET: /StoreManager/Edit/5

public ActionResult Edit(int id)
{
    var viewModel = new StoreManagerViewModel
    {
        Album = storeDB.Albums.Single(a => a.AlbumId == id),
        Genres = storeDB.Genres.ToList(),
        Artists = storeDB.Artists.ToList()
    };

    return View(viewModel);
}

所以在我看来,模型是在控制器中构建的,这对我来说是合乎逻辑的。然后在视图中他们使用这句话引起了我的困惑:

<%: Html.EditorFor(model => model.Album, new { Artists = Model.Artists, Genres = Model.Genres}) %>

该模型正在拆分/更改,现在我们将其他(艺术家,流派)作为ViewData发送? 有人可以解释一下,这完全符合整个设计模式吗?

1 个答案:

答案 0 :(得分:3)

您可以在控制器操作中放置断点并分析模型和查看数据。

这就是说,为什么你同时使用强类型视图和ViewData?确保ViewData["Models"] as IEnumerable不为null(在您的控制器中)或者甚至更好地删除它并将其作为强类型属性放入模型中。另外,我建议您使用强类型助手DropDownListFor

<%: Html.DropDownListFor(
    x => x.ModelID, 
    new SelectList(Model.Models, "ModelId", "Model", Model.ModelID)
)%>