开发者

asp.net mvc create a c# class in an ascx file for design purposes

I am developing a web application using asp.net mvc.

I have come across the need to Temporarily create a class in the ascx/aspx file.

This class will replace the Model during the development of the page.

开发者_开发百科It will also hold some test data for the user to have the chance to see some results.

Once we are happy with the layout on the screen, I will inherit the correct Model class through the Control tag.

Can you please advise if this is possible and how to do it?

This does not work:

<%
    class Modelo
    {
        public Guid Guid { get; set; }
        public string Name { get; set; }
    }
%>

Edit:

I have found that when I am sitting with the user and we are discussing the specifics of a control on the screen, in many cases when he/she sees the results and we discuss them he/she asks to change something. Moving between the controller, the model and the fake data repository consumes time. Even though the user is quit intelligent he/she can not follow what I am doing and feels less involved. If every thing was in one place I could explain to them what I am doing , they would at least feel that they know what is happening and we spend more time working and less time waiting for me to switch between screens. After he/she leaves I can sit down quietly and implement the agreed solution quietly. By keeping a class with fake data representing the model on the actual control all I will need to do is some housekeeping to meet the MVC way of doing things.

Hope it is more clear now.

Thanks in advance, Be happy - Julian


An alternative approach that would meet your needs could be as follows:

You base your view on a ViewModel as you would normally, however you copy the view model into a 'DisplayModel' local variable that you use for display purposes. The values in this can be set locally during the development demo stage - if no values have been supplied by the controller.

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MyViewModel>" %>

<%
  MyViewModel displayModel = MyViewModel;

  if (displayModel == null) 
  {
     displayModel = new MyViewModel {
        Property1 = "Test Value 1",
        Property2 = 100.00,
        Property3 = true
     };
  }
%>

<%= displayModel.Property1 %>
<%= displayModel.Property2 %>
<%= displayModel.Property3 %>


Why don't you directly hardcode the values in the view? Another possibility is to use the real model class but use a dummy repository inside the controller which will fill the model with temporary data. Once the final repository is ready all you have to do is instruct your DI framework to pass the real repository to the controller.

IMHO creating temporary classes inside the views wouldn't bring much value nor you will gain time.


Well, you can't really achieve what you want, as the view processor is not exactly like a code behind.

Your best bet would be to create a temporary class in your controller, and then pass that to the view. At least it takes some steps out of the way, and it shouldn't take too long to switch between only two windows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜