Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

.NET Webforms => MVC, General Discussion...

  • 25-11-2013 3:04pm
    #1
    Closed Accounts Posts: 83 ✭✭


    Hi Folks,

    I decided to go at building a mobile site for my company website, and for my desktop site, I developed that using .NET webforms and got used to working with controls such as asp:button, asp:label, and those sort of controls, also GridView & DetailsView, etc, etc. My website involves a lot of data display and transactional sort of information, so I've become fairly adept at binding filtered data to GridViews, etc.

    Anyway, back to the current matter at hand, over the weekend, I downloaded a sample mobile website, which was developed using MVC. I've been able to edit the css files and the content files to give myself a really handy looking mobile website.

    The only thing I'm not "getting" at the mo, is the way the solution doesn't contain any aspx files or controls such as asp:Label or asp:Button, instead it comprises of a chtml file and what I would consider to be "old" html controls such as <a href>...

    On first exposure to MVC, I'm just not "getting it", I'm not seeing how I can use the experience I have with .NET, as in accessing controls programmatically, from a codebehind page etc, and managing what controls and what data the user can see and use, I'm not seeing how it all hangs together using MVC. Even something as simple as putting a text label on a page and controlling it with a button, doesn't seem to be as easy as it was using webforms and an aspx page?

    I guess I'm asking would anyone have any advice for an MVC virgin?!?


«1

Comments

  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭John_Mc


    I'm not being smart, but no reply on here is going to give you more information that the ASP.net MVC site


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    John_Mc wrote: »
    I'm not being smart, but no reply on here is going to give you more information that the ASP.net MVC site

    I've watched 2 hours of video tutorials here on that site and I haven't seen a single thing that looks familiar to me from building a .NET website previously using webforms. Not giving out but it is a bit daunting!


  • Registered Users, Registered Users 2 Posts: 11,990 ✭✭✭✭Giblet


    You don't need to access controls at all any more, you are instead responsible for the data these controls use.
    You need to build up the model of the pages you want to render, and pass them to the views (which act as templates that use the data).
    There are helpers which can render controls for you (ie: @Html.Checkbox), but most of those are used in the context of forms.
    You can also pass data back to controllers using forms, querystring, JSON and much more. The idea of UserControls moves to the idea of Partials which are snippets of HTML which can be reused.

    In WebForms, you have one giant form, and every control posts back it's state to the server and you have a mess of JavaScript and other things to support this. In MVC there is no requirement to use forms, except when you actually need a form. A button is now a link to a href, or a submit action for a form, radio buttons really only need to exist in the context of forms, or can have their behaviour overridden in JavaScript (which you write) to perform some other action.

    The MVC framework also goes a lot further in allowing you to take complete control of the pipeline of events in a request, and allows things like Dependency Injection and IOC to work out of the box.

    If you need some specific examples on how to perform similar tasks to WebForms, feel free to post here and I'll try rustle something up.


  • Registered Users, Registered Users 2 Posts: 14,378 ✭✭✭✭jimmycrackcorm


    Op, you should be using JQuery UI for example instead. you'll then get plenty of super options that far exceed web form controls.


  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭John_Mc


    I agree with you Jimmy, but might be better to let the OP get his head around the MVC pattern and framework first.

    Once he does and combines it with the likes of Bootstrap, jQuery, Knockout etc, he'll start to shudder when he thinks of Webforms!


  • Advertisement
  • Closed Accounts Posts: 83 ✭✭LordSinclair


    John_Mc wrote: »
    I agree with you Jimmy, but might be better to let the OP get his head around the MVC pattern and framework first.

    Once he does and combines it with the likes of Bootstrap, jQuery, Knockout etc, he'll start to shudder when he thinks of Webforms!

    Thanks for the replies lads, I started off in this world back in the late 90's with very simple HTML form type markup. Then I started rummaging around with ASP when it came along and the transition from building a website using HTML code, to developing more complex data driven websites using ASP.NET (if that makes any sense), that transition from HTML code (buttons and hyperlinks and textboxes, etc!), to ASP.NET user controls and data management in the form of GridViews, etc, it seemed a bit more intuitive or something. In that I used very basic forms when I started HTML, and then used more a more complicated framework in .NET, but it was easy to see how one followed the other.

    Many years later, moving from .NET webforms to MVC seems to be a very different type of transition. As a poster said above, I should immerse myself into it a bit more and maybe for the moment, not try to carry anything over from how I developed using webforms.


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    Right, I've fallen at the first hurdle! How to create a new view (I would have called it a new page previously!), and navigate from one view to another, which I would have previously referred to, (using asp.net webforms), as creating a link and navigating from one page to another!

    So what did I do, well I downloaded a sample project from the web and uploaded it to my sample web domain that I use for testing, and the index page for my mobile view loads perfectly, and actually looks great on my iPhone after I edited the background CSS, and added in a company sample logo, etc...

    I then created a new view (copied and pasted it from the Index.Mobile.cshtml file), and renamed it AboutUs.Mobile.cshtml

    I then updated my Home Controller to as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace MobileSiteTest1.Controllers
    {
        public class HomeController : Controller
        {
            //
            // GET: /Home/
    
            public ActionResult Index()
            {
                return View();
            }
    [COLOR=Red]
            public ActionResult AboutUs()
            {
                return View();
            }[/COLOR]
    
        }
    }
    
    

    I then went into my View for Index.Mobile.cshtml and updated it to put in a link into my new AboutUs view, as follows:
    @{
        ViewBag.Title = "Index";
    }
    
    <div class="logo"><h1><a href="index.html"><img src="~/Content/mobile/images/logo.png" alt="logo" /></a></h1></div>
    <div class="menu">
        <a href="/Home/AboutUs"><b>Bring me to the About Us Page</b></a>
    
    </div>
    <h2 class="head"></h2>
    <div class="content">
    <p>Some text goes in here</p>
    </div>
    <div class="footer">
        <p>Some more text can go here...</p>
    </div>
    

    I've rebuilt the solution and uploaded the entire project (just in case some DDL files needed to be updated), and I'm getting a 404 error saying cannot find the page?

    I'd have thought after a few evenings of asp.net/mvc tutorials I'd have been able to manage this much?

    The logic in my head here is that I have created a new view, and updated the HomeController to call & display my new view (AboutUs), when the link is clicked in the Index.Mobile.cshtml file?

    The AboutUs page is definitely there...

    Sadly I tend to trip over the simple stuff and the stuff down the road falls into place easily enough!

    Any suggestions?!?


  • Registered Users, Registered Users 2 Posts: 11,990 ✭✭✭✭Giblet


    That's the framework tripping you up.
    When you use a View.Mobile.cshtml file, you need a View.cshtml file.
    Make a file named AboutUs.cshtml

    The .Mobile is an override for mobile only, you don't need it if you are doing mobile first, or responsive.


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    Giblet wrote: »
    That's the framework tripping you up.
    When you use a View.Mobile.cshtml file, you need a View.cshtml file.
    Make a file named AboutUs.cshtml

    The .Mobile is an override for mobile only, you don't need it if you are doing mobile first, or responsive.

    Hi Giblet, I did that, it's there and all I can see it, AboutUs.cshtml

    this is a mobile site though, the Index.cshtml is rendering fine on my phone but the other AboutUs.cshtml is generating a 404 error...


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    Have you made an AboutUs view?

    The framework uses convention over configuration so it expects files in certain places with certain names for it to all work. That's the key to getting it, I think.

    Behind the scenes there's a lot going on that you only need to worry about later.


  • Advertisement
  • Closed Accounts Posts: 83 ✭✭LordSinclair


    Have you made an AboutUs view?

    The framework uses convention over configuration so it expects files in certain places with certain names for it to all work. That's the key to getting it, I think.

    Behind the scenes there's a lot going on that you only need to worry about later.

    Yeah I created a file called AboutUs.cshtml in my Views/Home folder, isn't that the "View"? I would previously have called this a "new page" but am trying to get into the habit of looking at MVC very differently than how I've looked at webforms...


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    Yeah, that's the view. If you put a breakpoint in the AboutUs method of the controller, does it get hit? If you step into the view method, what view does it try to load?


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    Yeah, that's the view. If you put a breakpoint in the AboutUs method of the controller, does it get hit? If you step into the view method, what view does it try to load?

    I haven't got that far with MVC yet and as I haven't yet put a mobile emulator onto my laptop, I'm using my iPhone to "debug" the thing, as in just see if it is working, which it is until IO add my view. I downloaded the Safari mobile emulator which for some reason that I can't understand, doesn't have the iPhone in the list of mobile devices that it can emulate?!?


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    But it's not a mobile specific framework and debugging isn't MVC only either.

    Is there an AboutUs.Mobile.cshtml file? Does it work on a desktop web browser?


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    But it's not a mobile specific framework and debugging isn't MVC only either.

    Is there an AboutUs.Mobile.cshtml file? Does it work on a desktop web browser?

    282130.png

    There it is in all its glory, do you think I can get it to open though lol, not a chance! The Index.Mobile View will load on my iPhone, but I can't get any of the mobile views to display on the local computer, or on the remote server, even though the Index.Mobile page displays fine...


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    How about if there's an AboutUs.cshtml file too? Just thinking of the convention over configuration thing, I've never made separate views for mobile myself!


  • Registered Users, Registered Users 2 Posts: 11,990 ✭✭✭✭Giblet


    I don't see AboutUs.cshtml there, is it included in the solution?


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    Giblet wrote: »
    I don't see AboutUs.cshtml there, is it included in the solution?

    Well I just copied & pasted the Index.Mobile.cshtml file into the same folder & renamed it AboutUs.Mobile.cshtml, the working assumption bring that if this file loads on my iPhone, then a copied version of it would load, I just changed the content text to differentiate them. I'm just trying to start my mvc journey here by creating a new page & a hyperlink to it, it should be easier than "hello world" lol!

    Have to say it just feels plain wrong going back to using old HTML markup like <a href>, etc.


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    How about if there's an AboutUs.cshtml file too? Just thinking of the convention over configuration thing, I've never made separate views for mobile myself!

    I didn't create this solution myself, I just downloaded it to fiddle with it and see if I could start getting my head around mvc, it would be easier I think if I was coming at it cold rather than coming at it from an aspx/webforms direction.


  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭John_Mc


    Why not start from scratch? That's better than jumping into a solution someone else has created. The boiler plater MVC solution is easy to follow and structured in the way M$ recommend


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,021 ✭✭✭ChRoMe


    John_Mc wrote: »
    M$

    :rolleyes:


  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭John_Mc


    ChRoMe wrote: »
    :rolleyes:

    Location: Facking London Bruv :rolleyes:


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    John_Mc wrote: »
    Why not start from scratch? That's better than jumping into a solution someone else has created. The boiler plater MVC solution is easy to follow and structured in the way M$ recommend
    +1, MVC is all about being guided by the framework and doing things it's way. When I first tried MVC I started with an existing project and tried just adding my own stuff to it randomly and I got more and more confused.

    It's much better to start from scratch and follow a basic tutorial or two. It'll click in no time and then you can jump straight to the more advanced stuff.


  • Registered Users, Registered Users 2 Posts: 11,990 ✭✭✭✭Giblet


    Well I just copied & pasted the Index.Mobile.cshtml file into the same folder & renamed it AboutUs.Mobile.cshtml, the working assumption bring that if this file loads on my iPhone, then a copied version of it would load, I just changed the content text to differentiate them. I'm just trying to start my mvc journey here by creating a new page & a hyperlink to it, it should be easier than "hello world" lol!

    Have to say it just feels plain wrong going back to using old HTML markup like <a href>, etc.

    Index.Mobile.cshtml has an Index.cshtml too, so AboutUs.Mobile.cshtml needs a AboutUs.cshtml to work as well.

    If i were you I wouldn't use the .Mobile views at all, you don't need them, even if you are creating a mobile application, it's just some scaffolding to allow multiple html to be output depending on if you use mobile or not, you can just output your mobile view by default in AboutUs.cshtml.

    There's nothing wrong with "old" html controls, sure isn't that what the browser uses? HTML is just a view, you don't need backing logic to dictate how it renders, that is a front end concern only, you only care about how you get the data to the frontend in MVC, not what happens after. You can't even delete controls from a page in WebForms without changing the code sometimes!


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    stevenmu wrote: »
    +1, MVC is all about being guided by the framework and doing things it's way. When I first tried MVC I started with an existing project and tried just adding my own stuff to it randomly and I got more and more confused.

    It's much better to start from scratch and follow a basic tutorial or two. It'll click in no time and then you can jump straight to the more advanced stuff.

    Well, my prob is that I have a fairly active aspx/webforms/desktop site that I manage and it takes up a lot of my time. I downloaded this solution just to see if it would upload to my remote test domain and actually run/load from there. I was surprised when it did so successfully, then I started editing the CSS and content and within 30 minutes and uploaded the changed code, was amazed to find that I had basically cobbled together a mobile (simplified) version of my site, it obviously had no functionality or logic, but I was very happy with the front end.

    On the back of those small victories, I decided to stick with what I had downloaded and see if I could continue tinkering around with it, but adding a view has kinda gotten in the way of that. I remember running into the same kinda frustrations when I moved from HTML simple mark up back in my .htm days, onto aspx pages, the C# codebehind page at first actually frightened the shyte out of me haha, now generating controls progmatically, etc is second nature to me.


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    Giblet wrote: »
    Index.Mobile.cshtml has an Index.cshtml too, so AboutUs.Mobile.cshtml needs a AboutUs.cshtml to work as well.

    Gonna give this a shot now...
    Giblet wrote: »
    There's nothing wrong with "old" html controls, sure isn't that what the browser uses? HTML is just a view, you don't need backing logic to dictate how it renders, that is a front end concern only, you only care about how you get the data to the frontend in MVC, not what happens after. You can't even delete controls from a page in WebForms without changing the code sometimes!

    Ah I hear ya, it just seems counter intuitive when coming at it from aspx controls, naming a label or a text box without giving it an ID, etc, it feels like I'm doing it wrong, I know I'm not, it's all in me head as the man says!


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    Ok my AboutUs desktop page is now loading on my local dev environment, but my AboutUs.Mobile view is not loading on my remote domain, the only view I actually need to see...

    My HomeController doesn't need a different view for my mobile view, for the mobile view for the index.cshtml, it just has this in the file:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace MobileSiteTest1.Controllers
    {
        public class HomeController : Controller
        {
            //
            // GET: /Home/
    
            public ActionResult Index()
            {
                return View();
            }
    
            public ActionResult AboutUs()
            {
                return View();
            }
    
        }
    }
    

    Can't work out why something as simple as just adding a view is so utterly convoluted?!?


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    Can't work out why something as simple as just adding a view is so utterly convoluted?!?

    It shouldn't be really, I think you're just bypassing the MVC way of doing it by copy-pasting. You should be able to right-click in solution explorer to add a view that way, and it should add everything that's needed and wire it all up correctly.

    http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    stevenmu wrote: »
    It shouldn't be really, I think you're just bypassing the MVC way of doing it by copy-pasting. You should be able to right-click in solution explorer to add a view that way, and it should add everything that's needed and wire it all up correctly.

    http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view

    Gonna give this a shot, looks like I may have done wrong for trying to do right with my copy & pasting... :rolleyes:


  • Advertisement
  • Closed Accounts Posts: 83 ✭✭LordSinclair


    This just gets better and better, I have 2 views now that WILL OPEN in ViewInspector, independently of one another, but when I try to open the second view (OurTeam), from the main page (Index), hyperlink, it says view not found. I would expect this if I put in the hyperlink myself, however I let intellisense do it ( (I mean I let intellisense let me pick it out of the list of files/views that it can link to)?!?!?

    282212.png


  • Registered Users, Registered Users 2 Posts: 11,990 ✭✭✭✭Giblet


    Try
    <div class="menu">
        @Html.ActionLink("We want to go to our team","OurTeam","Home")
    </div>
    


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    Giblet wrote: »
    Try
    <div class="menu">
        @Html.ActionLink("We want to go to our team","OurTeam","Home")
    </div>
    

    Thanks a mil for that Giblet, that has sorted out the link issue on the local server, tried to upload it and none of it would work, have deleted it all and rebuilt the solution and trying to upload it all again now... It loaded fine before I started trying to add in a view!


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    Yeah the prob appears to be that my hosting crowd have me running on .NET 4.0 and I had to set up Visual Studio to run it on 4.5 to get the "View in Page Inspector" to work... I've checked the admin panel for my web host domain and it says running on 4.0, whereas the solution has been built on 4.5. Probably a morning job to get it sorted...

    Huge thanks for all the help on thread though and the patience!


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    I bumped my target framework back down to 4.0 on my local server, rebuilt and uploaded and YAYYYYYY, I've two pages that will load and one that links to the other!

    Just wondering is there any particular reason why a very simple <a href> wouldn't work with this solution, I mean is there anything here I need to take a lesson on?


  • Registered Users, Registered Users 2 Posts: 11,990 ✭✭✭✭Giblet


    It would work fine :D
    <a href="~/Home/OurTeam">Text></a>

    Html.ActionLink can help resolve the correct URL when custom routes are in use (you'll see more about them later).


  • Advertisement
  • Closed Accounts Posts: 83 ✭✭LordSinclair


    Giblet wrote: »
    It would work fine :D
    <a href="~/Home/OurTeam">Text></a>

    Html.ActionLink can help resolve the correct URL when custom routes are in use (you'll see more about them later).

    I would never have run into this shyte with an aspx page! I mean once you appreciate the importance of runat="server" :D:D:D


  • Moderators, Science, Health & Environment Moderators Posts: 9,035 Mod ✭✭✭✭mewso


    LordSinclair I would recommend you read up on Domain Driven Design, Test Driven Development/Behaviour Driven Design, Dependency Injection and go from there. Throw in some reading on ORMs like nHibernate or Entity Framework. They are not necessary to work with MVC but they all work very well together.

    When I create Asp.Net MVC applications most Views are based on View Models which in turn are flattened versions of Domain objects mapped to the database. Controllers are injected with a view service interface implementation (using StructureMap) which builds the view models using nhibernate repositories to generate everything the model needs. By the time the view is being populated from the model database access is no longer possible and the view will use html helpers wherever possible and I will add extensions to the html helpers where necessary to make the view as straightforward as possible. This all results in a cleaner application that I too first considered a backwards step when moving from web forms. If none of what I just said makes sense then I refer you to my first paragraph.

    The best series of videos I watched that helped me put a lot of it together was the storefront series which you may want to have a look at.

    *edit - meant to say I'm not that keen on the generated views in visual studio myself mainly due to personal taste. I want tables with theads (and accesibility attributes) and forms without pointless divs for each label input pairing (good css renders the div uneeded in most cases) as well as using definition lists (dl) for details views. It's still probably easier in general to let visual studio create them and work from there. If you used gridviews in webforms then you may not care as much as I came to about the html that gets outputted (I used repeaters) so this may not mean much.


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    Just wanted to say one thing abut MVC since I started using it. Simplicity of use seems to have been thrown completely and totally out the window with MVC, while on some level I can "get" the concept, yesterday I tried to put a simple <a href> in a view, it wouldn't link to another view. On my pages, I had a small banner image and wanted to <a href> this image to the main page, yet again, it was anything but a simple <a href> I had to use the following to link a banner...
    <div class="logo">@Html.ActionLink(
        " ", 
        "Index", 
        "Home", 
        new { 
            style = "background: url('" + Url.Content("~/Content/mobile/images/logo.png") + "') no-repeat center; display:block; height:90px; width:300px;" 
        }
    )
        </div>
    

    I know the focus with MVC is on control whereas with webforms it was automation, but I was hoping the simple tasks would still be "simple", am I wrong in seeing it like this, that with MVC, the simple has been upgraded to complicated, html helpers required for an <a href>, etc, etc?

    Not trying to start a dispute on here btw, just being devils advocate!


  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭John_Mc


    Just wanted to say one thing abut MVC since I started using it. Simplicity of use seems to have been thrown completely and totally out the window with MVC, while on some level I can "get" the concept, yesterday I tried to put a simple <a href> in a view, it wouldn't link to another view. On my pages, I had a small banner image and wanted to <a href> this image to the main page, yet again, it was anything but a simple <h ref> I had to use the following to link a banner...
    <div class="logo">@Html.ActionLink(
        " ", 
        "Index", 
        "Home", 
        new { 
            style = "background: url('" + Url.Content("~/Content/mobile/images/logo.png") + "') no-repeat center; display:block; height:90px; width:300px;" 
        }
    )
        </div>
    

    I know the focus with MVC is on control whereas with webforms it was automation, but I was hoping the simple tasks would still be "simple", am I wrong in seeing it like this, that with MVC, the simple has been upgraded to complicated, html helpers required for an <a href>, etc, etc?

    Not trying to start a dispute on here btw, just being devils advocate!

    You have no idea how the framework works so it's a bit early to be dismissing it.

    You don't link to a view, you link to a controller which returns a view (or the data to populate your view).

    Try using ActionLink and this will render a link to a Controller Action.

    Trust me, when you understand how it all hangs together you'll like it.

    Also, I think you need to take a step back and start from scratch with a good tutorial. You're trying to jump ahead and that's why you're on here asking people how to do the most basic of things and getting frustrated.
    <div class="logo"><a href="@Url.Action("Index","Home")>
    <img src="@Url.Content("~/Content/mobile/images/logo.png")" /> </a> 
    )
        </div>
    


  • Registered Users, Registered Users 2 Posts: 11,990 ✭✭✭✭Giblet


    I think you need to take some tutorials. both MVC and HTML, you are doing some weird stuff.

    Linking a banner is as simple as
    <a href="~/Home/Index"><img src="~/Images/image.jpg" /></a>
    

    With some MVC flourish (this is not needed, it is just a helper, which you will appreciate more when you get into routing)
    <a href=@Url.Action("Index", "Home")><img src="~/Images/image.jpg" /></a>
    


  • Advertisement
  • Closed Accounts Posts: 83 ✭✭LordSinclair


    John_Mc wrote: »
    You have no idea how the framework works so it's a bit early to be dismissing it.

    I'm not dismissing it, just saying that simplicity of use appears to not have been a consideration in how it was developed. I tried that yesterday, just doing <a href> and putting the image inside the link, but it wouldn't work, just like it wouldn't like to other views in my solution until a poster on here suggested that I try ActionLink, which thankfully worked. Drawing from that experience, it seems that there are "contexts" here when it comes to MVC, where simple page/view controls work in one context, but maybe not so well in another. What I loved about webforms was that you could start off on the simple stuff and not run into these kinda obstacles, and build your knowledge from there. It seems that the knowledge required to get out of the traps with MVC is much higher. I'm doing a few tutorials now on it, but the way I usually start this is do a few simple tutorials, practice there with the simple stuff, start seeing some results, and then build from there.

    I'm not knocking the framework or dissing it as what I will admit is a brand new user to it, just thinking out loud!


  • Moderators, Science, Health & Environment Moderators Posts: 9,035 Mod ✭✭✭✭mewso


    <div class="logo">@Html.ActionLink(
        " ", 
        "Index", 
        "Home", 
        new { 
            style = "background: url('" + Url.Content("~/Content/mobile/images/logo.png") + "') no-repeat center; display:block; height:90px; width:300px;" 
        }
    )
        </div>
    

    Are you saying that you habitually spit style attributes out into your html or put empty links with css background images in them? Or is this just the only way you could get it to work? If so how would you have done this in web forms. Might make it clearer. Going from the above I would do something like this:-
    @Html.ActionLink("Home", "Index", "Home", new { @class = "logo" })
    

    and in your external stylesheet:-
    a.logo {
        background: url('../images/mobile/logo.png') no-repeat center; 
        display:block; 
        height:90px; 
        width:300px;
        text-indent: -999em;
    }
    

    If that is the only link that will be in that div then you can also do this:-
    @Html.ActionLink("Home", "Index", "Home")
    
    div.logo a {
        background: url('../images/mobile/logo.png') no-repeat center; 
        display:block; 
        height:90px; 
        width:300px;
        text-indent: -999em;
    }
    

    Wherever your css files are I would almost always have template related images in a folder alongside the styles folder so the stylesheet can always reference ../images and to me from that it would make more sense to have mobile as a sub-folder within the images folder. Switching to MVC is a good opportunity to get used to using all of the tools, html, css and so on as well as code to achieve what you want and not stick to bad habits. Working with MVC I find myself consistently thinking of the best way to do something rather than the easiest way and because of the improvements all round (less repition and more readable code) it ends up the easiest in the end too.

    If you are having problems with something then post up how you used to do it and I would be happy to say how I do it in MVC.


  • Moderators, Science, Health & Environment Moderators Posts: 9,035 Mod ✭✭✭✭mewso


    I'm not dismissing it, just saying that simplicity of use appears to not have been a consideration in how it was developed. I tried that yesterday, just doing <a href> and putting the image inside the link, but it wouldn't work, just like it wouldn't like to other views in my solution until a poster on here suggested that I try ActionLink, which thankfully worked. Drawing from that experience, it seems that there are "contexts" here when it comes to MVC, where simple page/view controls work in one context, but maybe not so well in another. What I loved about webforms was that you could start off on the simple stuff and not run into these kinda obstacles, and build your knowledge from there. It seems that the knowledge required to get out of the traps with MVC is much higher. I'm doing a few tutorials now on it, but the way I usually start this is do a few simple tutorials, practice there with the simple stuff, start seeing some results, and then build from there.

    You may need to read the concepts a bit more. You don't link to the physical view files. You link to actions that know what view to send back. First question you ask yourself is "Is there a html helper to do this?" and if not is there an alternative. And you can alwas fall back on html:-
    <a href="@Url.Action("Index", "Home")"><img src="@Url.Content("~/Content/Images/myimage.jpg")" /></a>
    

    When the actionlink won't work the Url object gives you options like give me the url for the Index action on the Home Controller and give me the relative url to the images folder as above. Not to mention adding extensions to the helper methods to suit your own scenarios.


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    mewso wrote: »
    When the actionlink won't work the Url object gives you options like give me the url for the Index action on the Home Controller and give me the relative url to the images folder as above. Not to mention adding extensions to the helper methods to suit your own scenarios.

    But this is kinda the point I'm making, is this not all a little bit over the top just to set up a link to an image or to another view?

    I know I'm not making sense as a new user to the framewoprk, I'll say no more until I go through a few more tutorials, but have to say, the help on thread with just getting me to a place where I can upload views that I can edit and fumble around with, has been hugely helpful. This isn't everyone's starting point for learning a new framework, but it is where I have started with it, if I wasn't able to get the basic results that I have gotten with it in the few days that I've been rooting around with it, creating views and seeing it work properly on a web test domain, I'd have thrown in the towel before I was able to get anywhere with it, I'm impatient & fickle like that! :D


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    LordSinclair, I think a lot of problems you are encountering comes not understanding the separate layers of the Model, View,Controller and also Routing.

    Maybe have a read of a short article like this, to understand how your request goes through various steps to turn into a response.

    http://www.dotnet-tricks.com/Tutorial/mvc/TbR0041112-Asp.net-MVC-Request-Life-Cycle.html


  • Moderators, Science, Health & Environment Moderators Posts: 9,035 Mod ✭✭✭✭mewso


    But this is kinda the point I'm making, is this not all a little bit over the top just to set up a link to an image or to another view?

    In both Web forms and MVC the difference is minimal really:-
    <asp:hyperlink id="mylink" runat="server" navigateurl="~/myfolder/home.aspx">Home</asp:hyperlink>
    
    or
    
    @Html.ActionLink("Home", "Index", "Home")
    

    Now thats not a huge difference. The only issue being you need to embrace the idea of linking to actions not actual files. When you want an image link you can do as my previous post or even create a helper extension that is useful from anywhere from that point on.

    With web forms you would probably throw in an image control and set the properties and have a nice image in a link with inline style attributes generated by asp.net whether you like it or not. Not my preference but I can understand why you might disagree. It's always fun, for example, to set the cssclass value of a checkbox in web forms and see a span with that class value wrap the checkbox in the outputted html. Like I said at the start it might be a good time to look at how the whole package of web development works together. Web standards, web accessibility and so on. So that you will appreciate what was bad about web forms before you condemn mvc.

    Microsoft have a poor track record in these areas, typically jumping on the bandwagon to pay some lip service to standards and accessibility but never doing a thorough enough job with it. Asp.net mvc means I can be less bothered about that because I have more control. I can create helpers that generate image links the way I want them and re-use it in every application after that. I can create tables with header rows and id attributes that can be referenced by header attributes in the row cells. I can rely on my html elements having consistent id values because they haven't been renamed by the containing object (masterpage/user control). Etc.


  • Registered Users, Registered Users 2 Posts: 11,990 ✭✭✭✭Giblet


    mewso wrote: »
    It's always fun, for example, to set the cssclass value of a checkbox in web forms and see a span with that class value wrap the checkbox in the outputted html.

    Ah, my favourite :D


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    Thanks again for the help everyone, helping me get on the first step of the ladder has been hugely helpful.

    I've a load road ahead of me with this, as I've a lot of developed code on my website that took me ages to put together, it is heavily integrated and formatted/themed on the front end with controls such as GridView. I've also a lot of C# code in my webforms based solution that connects with 3rd party sites for payment purposes, and data checking purposes.

    Having to take this, and sort it all out into Model-View-Controller, is a bit daunting to be honest! But I do need to get to a place where I have one solution that provides a desktop site and a mobile site, and not what I have at the minute, which is a desktop site, which works very well, running on Webforms/aspx, and a mobile site (this is my new MVC project...


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    While I prefer MVC over Web Forms, I think you should consider if rewriting to MVC is the best choice here. This is of course will depend on the size of the application, and your time and budget and any other opportunities you forgo while developing the new solution.

    You could always use some of Web Forms techniques listed here to achieve a mobile app:

    http://www.asp.net/whitepapers/add-mobile-pages-to-your-aspnet-web-forms-mvc-application

    Of course, if you have the time, may as well go for it.


  • Closed Accounts Posts: 83 ✭✭LordSinclair


    While I prefer MVC over Web Forms, I think you should consider if rewriting to MVC is the best choice here. This is of course will depend on the size of the application, and your time and budget and any other opportunities you forgo while developing the new solution.

    You could always use some of Web Forms techniques listed here to achieve a mobile app:

    http://www.asp.net/whitepapers/add-mobile-pages-to-your-aspnet-web-forms-mvc-application

    Of course, if you have the time, may as well go for it.

    I'm hoping I'll pick it up easy enough once I get my head around the framework approach, and can start getting some of my current functionality in place using MVC. C# used to scare the shyte outa me, the idea of tapping into a GridView using an event handler used top scare the shyte outa me, but I eventually got myself beyond the feeling of being lost in what I was at and now have some really useful market leading functionality running on my webforms desktop website.

    But the future seems to be going increasingly mobile so this is a transition I probably need to make...


  • Advertisement
Advertisement