简体   繁体   English

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

[英]Debugging code in a view (asp.net mvc2)

How do I debug code in the View in asp.net mvc2 application? 如何在asp.net mvc2中的视图应用程序中调试代码?

Edit after progress last night : 昨晚经过进度编辑

Ok so now I have the following: 好吧,现在我有以下内容:

in Shared\\EditorTemplates\\Equipment.ascx : 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>

        ...

in Item\\Edit.aspx : 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>

The controller: 控制器:

    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);
    }

So I am sure that the problem is with this line 所以我确定问题出在这条线上

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

When generating the selectlist, I don't have IEnumerable for the first parameter? 生成选择列表时,第一个参数没有IEnumerable吗? Or one of the values I am feeding this is causing null. 或者我输入的值之一导致空值。 How do I get the list of models in my view? 如何在我的视图中获取模型列表?

EDIT AFTER PULLING ALL MY HAIR OUT: 拔出所有头发后进行编辑:

It seems the problem lies in this example: http://www.asp.net/mvc/tutorials/mvc-music-store-part-4 . 看来问题出在以下示例中: http : //www.asp.net/mvc/tutorials/mvc-music-store-part-4 Strangely, I am not sure if it is following best practice. 奇怪的是,我不确定它是否遵循最佳实践。 Look at the code and how they pass the models about - it seems stupidly obscure using ViewData["Blah"] and the Models as well, why can't you just have it all sent as the model? 看一下代码以及它们如何传递模型-使用ViewData [“ Blah”]和模型也显得晦涩难懂,为什么不能将它们全部作为模型发送? Look at the code how they have done it: 看一下代码他们是如何做到的:

Album.ascx : 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>

View: 视图:

<%@ 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>

View Model: 查看模型:

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; }
    }
}

And the controller: 和控制器:

//
// 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);
}

So it looks to me like the model is built in the controller as makes logical sense to me. 因此,在我看来,模型内置在控制器中对我来说是合乎逻辑的。 Then in the view they use this statement which causes my confusion: 然后在视图中,他们使用此语句,这引起了我的困惑:

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

The model is being split/changed and now we send the other (Artists, Genres) as ViewData ?? 该模型正在拆分/更改,现在我们将其他模型(艺术家,流派)作为ViewData发送? Can someone explain, and is this at all fitting with the entire design pattern? 有人可以解释,这完全适合整个设计模式吗?

You could put a breakpoint in your controller action and analyze your model and view data. 您可以在控制器动作中设置一个断点,并分析模型并查看数据。

This being said, why are you using a strongly typed view and ViewData at the same time? ViewData ,为什么要同时使用强类型视图和ViewData Make sure that ViewData["Models"] as IEnumerable is not null (in your controller) or even better get rid of it and put it in your model as a strongly typed property. 确保ViewData["Models"] as IEnumerable不为null(在您的控制器中),或者最好将其除去,并将其作为强类型属性放入模型中。 Also I would recommend you using the strongly typed helper DropDownListFor : 我也建议您使用强类型的辅助器DropDownListFor

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM