簡體   English   中英

在視圖中調試代碼(asp.net mvc2)

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

如何在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嗎? 或者我輸入的值之一導致空值。 如何在我的視圖中獲取模型列表?

拔出所有頭發后進行編輯:

看來問題出在以下示例中: 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發送? 有人可以解釋,這完全適合整個設計模式嗎?

您可以在控制器動作中設置一個斷點,並分析模型並查看數據。

ViewData ,為什么要同時使用強類型視圖和ViewData 確保ViewData["Models"] as IEnumerable不為null(在您的控制器中),或者最好將其除去,並將其作為強類型屬性放入模型中。 我也建議您使用強類型的輔助器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