Debugging code in a view (asp.net mvc2)

How do I debug code in the View in asp.net mvc2 application?Edit after progress last night:Ok so now I have the following:in SharedEditorTemplatesEquipment.ascx :<%@ Control Language=”C#” Inherits=”System.Web.Mvc.ViewUserControl<DAT.Models.Item>” %> <% using (Html.BeginForm()) %> <%: Html.ValidationSummary(true) %> <%: Html.Label(“Item ID”) %> <%: …

via C Programming Language Tutorial » Search Results » ajax:

Debugging code in a view (asp.net mvc2)

How do I debug code in the View in asp.net mvc2 application?
Edit after progress last night:
Ok so now I have the following:
in SharedEditorTemplatesEquipment.ascx :
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl” %>

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

<%: Html.Label("Item ID") %>
<%: Html.TextBoxFor(model => model.ItemID) %>
<%: Html.ValidationMessageFor(model => model.ItemID) %>
<%: 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) %>

…in ItemEdit.aspx :
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage” %>

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

Fields

<%: Html.EditorFor(model => model.Item) %>

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? 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 . 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? Look at the code how they have done it:
Album.ascx :
<%@ Import Namespace="MvcMusicStore"%>

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



<%: Html.LabelFor(model => model.Title)%>
<%: Html.TextBoxFor(model => model.Title)%>
<%: Html.ValidationMessageFor(model => model.Title)%>

<%: Html.LabelFor(model => model.Price)%>
<%: Html.TextBoxFor(model => model.Price)%>
<%: Html.ValidationMessageFor(model => model.Price)%>

<%: Html.LabelFor(model => model.AlbumArtUrl)%>
<%: Html.TextBoxFor(model => model.AlbumArtUrl)%>
<%: Html.ValidationMessageFor(model => model.AlbumArtUrl)%>

<%: Html.LabelFor(model => model.Artist)%>
<%: Html.DropDownList("ArtistId", new SelectList(ViewData["Artists"] as IEnumerable, "ArtistId", "Name", Model.ArtistId))%>

<%: Html.LabelFor(model => model.Genre)%>
<%: Html.DropDownList("GenreId", new SelectList(ViewData["Genres"] as IEnumerable, "GenreId", "Name", Model.GenreId))%>

View:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage” %>


Edit – <%: Model.Album.Title %>

Edit Album

<% Html.EnableClientValidation(); %>

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

Edit Album

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

<% } %>

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


View Model:
using System.Collections.Generic;
using MvcMusicStore.Models;

namespace MvcMusicStore.ViewModels

public class StoreManagerViewModel

public Album Album get; set;
public List Artists get; set;
public List 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 ?? 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? 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. Also I would recommend you using the strongly typed helper DropDownListFor:
<%: Html.DropDownListFor(
x => x.ModelID,
new SelectList(Model.Models, “ModelId”, “Model”, Model.ModelID)
)%>

For more info: Debugging code in a view (asp.net mvc2)

C Programming Language Tutorial » Search Results » ajax

Debugging code in a view (asp.net mvc2)

Share this post:

Related Posts

Leave a Comment