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

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

Options
2»

Comments

  • Registered Users Posts: 11,979 ✭✭✭✭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 Posts: 11,979 ✭✭✭✭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: 8,919 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 Posts: 2,791 ✭✭✭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 Posts: 11,979 ✭✭✭✭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: 8,919 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: 8,919 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 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: 8,919 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 Posts: 11,979 ✭✭✭✭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 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
  • Registered Users Posts: 1,266 ✭✭✭Overflow


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

    Huh? At the end of the day its all HTML code, whether your using asp controls or not. There is nothing old about it.

    You just suffering the .net webforms paradigm. It has warped you understanding of how HTTP and HTML truly work. It would be in your best interest to forget everything you ever learn about webforms.


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


    Overflow wrote: »
    Huh? At the end of the day its all HTML code, whether your using asp controls or not. There is nothing old about it.

    You just suffering the .net webforms paradigm. It has warped you understanding of how HTTP and HTML truly work. It would be in your best interest to forget everything you ever learn about webforms.

    Totally agree with this.

    HTTP is stateless. Webforms uses Viewstate and the Page Lifecycle to get around this.

    Unlike your code behind which is aware of the controls on the page, your controller action can only work with what it is given via form posts.


Advertisement