Basic questions concerning the MVC framework overall

Last post 07-04-2009 12:53 PM by liaothomas. 12 replies.

Sort Posts:

  • Basic questions concerning the MVC framework overall

    02-01-2008, 6:09 AM
    • Member
      21 point Member
    • Jeinhor
    • Member since 12-16-2006, 2:56 PM
    • Posts 38

    Hello,

     I've read the excellent blog series by ScottGu concerning the MVC framework and I now have the following questions:

    • How will I handle file uploads?
    • I don't fully understand why I can't use ordinary TextBoxes instead of the helper methods in MVC toolkit?
    • This is related to the above question, but what about viewstate controls? If I check the users input in a New-view and I find input errors I wish them to correct, will I manually have to preserve the entered values (as postback would've done for me)?
    • I often combine my New and Edit forms, but Scott seems to create different views for this. Is there any problem with MVC and combining those forms?
    It all seems very neat though!

     


     

  • Re: Basic questions concerning the MVC framework overall

    02-01-2008, 6:54 AM
    Answer
    • Contributor
      7,054 point Contributor
    • rjcox
    • Member since 12-19-2007, 2:14 PM
    • Basingstoke, UK
    • Posts 1,444

     Partial answers:

    Jeinhor:
    I don't fully understand why I can't use ordinary TextBoxes instead of the helper methods in MVC toolkit?
     

    You can. There is no requirement to use the helpers. 

    Jeinhor:
    This is related to the above question, but what about viewstate controls?
     

    No ViewState with MVC, so anything that depends on ViewState (and this includes many, if not most, server controls) is not usable with MVC.

     

    Jeinhor:
    often combine my New and Edit forms, but Scott seems to create different views for this. Is there any problem with MVC and combining those forms?

    You can (and I have), you'll likely want to look at how you label/title things and your new/create/edit/update actions will need to be consistent.

    Combining works well when you need the ability to send a "new" form back to the user due to invalid/incomplete input beforte creating the new entity. 

    Richard
  • Re: Basic questions concerning the MVC framework overall

    02-01-2008, 8:01 AM
    Answer
    • All-Star
      29,644 point All-Star
    • Fredrik N
    • Member since 06-22-2002, 5:03 AM
    • Sweden
    • Posts 5,334
    • TrustedFriends-MVPs

    rjcox have answerd most of the questions you have, but I will add something:

    >I don't fully understand why I can't use ordinary TextBoxes instead of the helper methods in MVC toolkit?

    When you use the TextBox control, you need to place it inside of a <form runat="server">, you also need to dynamically set the Text property in the code-behind to give it a value. You can use HTML Web Server Controls to avoid using the <form runat="server">. But I don't like the solution where there will be two files where we put logic which influence the rendering of the View, the .aspx page and the code-behind.

    /Fredrik Normén - fredrikn @ twitter

    ASPInsider

    Microsoft MVP, MCSD, MCAD, MCT

    ASPInsiders
    My Blog
  • Re: Basic questions concerning the MVC framework overall

    02-01-2008, 2:28 PM
    Answer
    • Contributor
      4,360 point Contributor
    • tgmdbm
    • Member since 12-17-2007, 2:08 PM
    • Posts 882
    • ASPInsiders
      TrustedFriends-MVPs

     cox, it seems you don't fully understand ViewState, i know this point is mute because we never have to worry about it anymore, but...

    In asp.net, when a page is built during a postback, each control gets its own viewstate which gets populated with the defaults of the control (defined by attributes), then they get overwritten by the declarative properties (as defined in  the aspx page). THEN the viewstate gets deserialised from the postback and the properties get overwritten again. then trackchanges is called and the rest of the lifecycle happens yadda yadda then the viewstate gets serialised back and output into the hidden field on the page.

    So even if you remove the viewstate postback, you still get a control where all the properties are either default, or as declared on the aspx page, or overwritten in code behind.

    They still work without viewstate, or at least, they would if they didn't explicitly require it (because viewstate is needed to respond to certain events.)


    Now you know, and knowing is half the battle.

    because KNOWLEDGE is POWER
     

  • Re: Basic questions concerning the MVC framework overall

    02-02-2008, 5:58 AM
    • Member
      21 point Member
    • Jeinhor
    • Member since 12-16-2006, 2:56 PM
    • Posts 38

    Thanks for you answers!

    The remaining question is about file upload, but I guess that could be handled with a comon HTML upload file control?

  • Re: Basic questions concerning the MVC framework overall

    02-02-2008, 6:21 AM
    • Member
      125 point Member
    • Mike343
    • Member since 03-30-2005, 7:59 AM
    • Posts 43

     This is actually really simple.

    HTML (View)

    <form action="" enctype="multipart/form-data" method="post">
    <input name="upload" type="file" />
    <br />
    <input type="submit" value="Upload" />
    </form>

    C# (Action)

    HttpPostedFile upload = HttpContext.Current.Request.Files["upload"];
     
  • Re: Basic questions concerning the MVC framework overall

    02-02-2008, 4:06 PM
    • Contributor
      7,054 point Contributor
    • rjcox
    • Member since 12-19-2007, 2:14 PM
    • Basingstoke, UK
    • Posts 1,444

    tgmdbm:
    they would if they didn't explicitly require i
     

    I.e. depend on it.

    Which is why I said that.

    (And was including third party control suites in the "many if not most").

    Richard
  • Re: Basic questions concerning the MVC framework overall

    02-20-2008, 3:28 PM
    • Member
      25 point Member
    • jgo23
    • Member since 12-06-2007, 5:52 PM
    • Posts 27

    Hi Mike (or maybe anyone else)...

    but have you tried out the snippet you provided (below)?

    Mike343:

     This is actually really simple.

    HTML (View)

    <form action="" enctype="multipart/form-data" method="post">
    <input name="upload" type="file" />
    <br />
    <input type="submit" value="Upload" />
    </form>

    C# (Action)

    HttpPostedFile upload = HttpContext.Current.Request.Files["upload"];

     

     When I tried it, The Files collection was always empty,but I could see the input coming across with the path.  I did get it to work finally by instead using the plain vanilla "<input type="file".... " element, I replaced it with the asp.net FileUpload user control, ie <asp:FileUpload ID="Avatar" runat="server" />.  With no code changes except for this replacement, I could then see the uploaded file from my controller.

    Can someone explain this?  From reading up on articles [1], I think it has something to do with actually needing to process it, and I think in this case, asp.net is processing it because of the server side control.

     [1] http://www.cs.tut.fi/~jkorpela/forms/file.html

  • Re: Basic questions concerning the MVC framework overall

    04-06-2008, 6:14 AM
    • Member
      4 point Member
    • armaSANEA
    • Member since 04-06-2008, 10:08 AM
    • Posts 2

     Hi, you stated that

    jgo23:
    With no code changes except for this replacement, I could then see the uploaded file from my controller.

    But I get an Parser error if I place an <asp:FileUpload runat="server"/> inside a form without runat="server":

     Control 'ctl00_MainContentPlaceHolder_DllFile' of type 'FileUpload' must be placed inside a form tag with runat=server.

    Can you post a complete source page for your file upload? Thanks!

     

  • Re: Basic questions concerning the MVC framework overall

    04-06-2008, 10:12 AM
    • Member
      10 point Member
    • tumickey
    • Member since 11-25-2007, 10:12 AM
    • Viet Nam
    • Posts 9
    upload with mvc :) http://forums.asp.net/t/1237419.aspx
  • Re: Basic questions concerning the MVC framework overall

    04-06-2008, 10:34 AM
    • Member
      25 point Member
    • jgo23
    • Member since 12-06-2007, 5:52 PM
    • Posts 27

    I also had to attribue the containing form with "runat=server", as well.  The view/controller still works and remains unaffected by doing this, however, after reading the link posted after yours, i think the reason why the non-asp.net input wasn't working for me was probably because I was missing the name attribute.  I just tried this method and it does work, so probably just a good idea to stay away from the FileUpload server control (although, it seems to work just fine).

  • Re: Basic questions concerning the MVC framework overall

    04-07-2008, 2:17 AM
    • Member
      4 point Member
    • armaSANEA
    • Member since 04-06-2008, 10:08 AM
    • Posts 2

    Placing a name attribute on the html file input did the trick. I'm going to stay away from runat="server" for now :)

    Thanks a bunch!

  • Re: Basic questions concerning the MVC framework overall

    07-04-2009, 12:53 PM
    • Member
      7 point Member
    • liaothomas
    • Member since 06-23-2009, 5:48 PM
    • Posts 5

    With respect to a common form between new and edit, the best solution that I have seen is the use of a partial view.  You can take all of the comon elements (logic and controls) and place them into a partial view.  For the partial to appear on a page you use the Html helper method, RenderPartial("PartialViewName").  The partial view is contained in an ASCX file.  The PartialViewName works the same way as view names (MyView.asmx for the same controller is View("MyView").  If the partial view is located in the same controler as MyView, my view contains <%= Html.RenderPartial("PartialViewName") %> (Assuming that the partial view file is named PartialViewName.ascx.

    You might want to work through one of the available tutorials for MVC 1.0 that are available on the wen.

Page 1 of 1 (13 items)