【问题标题】:Debugging code in a view (asp.net mvc2)在视图中调试代码 (asp.net mvc2)
【发布时间】:2010-08-23 06:54:20
【问题描述】:

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

昨晚进度后编辑

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

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"] 和模型似乎愚蠢地晦涩难懂,为什么不能将它们全部作为模型发送?看看他们是怎么做的代码:

专辑.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);
}

所以在我看来,模型是内置在控制器中的,这对我来说是合乎逻辑的。然后在他们看来,他们使用了这个导致我困惑的陈述:

&lt;%: Html.EditorFor(model =&gt; model.Album, new { Artists = Model.Artists, Genres = Model.Genres}) %&gt;

模型正在拆分/更改,现在我们将另一个(艺术家、流派)作为 ViewData 发送? 谁能解释一下,这是否完全符合整个设计模式?

【问题讨论】:

  • 你能告诉我们你的控制器中的动作方法吗?
  • 这里的 ViewData["Models"] 似乎为空。

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


【解决方案1】:

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

话虽如此,你为什么同时使用强类型视图和ViewData?确保ViewData["Models"] as IEnumerable 不为空(在您的控制器中),或者更好地摆脱它并将其作为强类型属性放入您的模型中。另外我建议你使用强类型帮助器DropDownListFor

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

【讨论】:

  • 我正在按照此处的示例进行操作,示例代码显示了这样做 asp.net/mvc/tutorials/mvc-music-store-part-4 。至于在控制器中设置断点,我做到了,AFAICT 我没有传递任何空值。
  • 您没有传递任何空值...但ViewData["Models"] as IEnumerable 可能是无效转换并返回空值。
猜你喜欢
  • 1970-01-01
  • 2011-02-16
  • 1970-01-01
  • 2011-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多