0

I hate those razor helpers. (LabelFor, TextboxFor...) they try to help me but they teach me nothing.

I want to try with ASPX engine. when I open it there is even toolbox on the left with all old good html commands. why can't i use it ?

how come that when i try to buil app intellisense say runat="server" is required when I know MVC doesn't need that ?

In brief how do I write HTML w/o using helpers ? Any constructive advice would be appreciated.

Sample:

<%@ Page Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">Home Page</asp:Content>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
   <asp:ListBox runat="server">
        <asp:ListItem Text="text1" />
        <asp:ListItem Text="text2" />
    </asp:ListBox>
</asp:Content>

Error:

`Server Error in '/' Application.
Control 'MainContent_ctl00' of type 'ListBox' must be placed inside a form tag with runat=server.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'MainContent_ctl00' of type 'ListBox' must be placed inside a form tag with runat=server.

Source Error:

Line 22:                 <ul id="menu">
Line 23:                     <li><%: Html.ActionLink("Home", "Index", "Home")%></li>
Line 24:                     <li><%: Html.ActionLink("About", "About", "Home")%></li>
Line 25:                 </ul>
Line 26:             </div>`
Jalle
  • 1,566
  • 5
  • 22
  • 42
  • So I see all experts are here but no one to answer 1 simple question: **How do you write Listbox control WITHOUT using Razor @HTML.listBox helper ?** – Jalle Aug 25 '12 at 09:49
  • if, ListBox using HTML is what you want, then here it is : http://fiddle.jshell.net/GWtCv/ – Yasser Shaikh Aug 25 '12 at 09:52

3 Answers3

1

You hate razor helpers, but you like to use predefined elements from the toolbox? Items in the toolbox are meant for webforms, but can be used for MVC as well. This is because MVC and webforms are still members of the asp.net family. However, I wouldn't really recommend to do that...

In brief how do I write HTML w/o using helpers ?

How do you write a simple text? Simply type the html tags as needed. You can use razor/aspx (doesn't really matter that much which one you choose) syntax to loop over your collections and construct the html based on your data. Nothing is forcing you to use razor helpers or webforms controls...

I think it may be the right time to learn a bit more about the technology you're using, because you seem to be confused about the really basic stuff here...

walther
  • 13,466
  • 5
  • 41
  • 67
  • I strongly disagree with the fact that you can use server side controls in MVC. Didn't you see the exception the OP got when he tried to do that? While you could still circumvent this by placing a `
    ` with `runat="server"` using the WebForms view engine, in Razor that's no longer possible.
    – Darin Dimitrov Aug 25 '12 at 09:26
  • @DarinDimitrov, it's your right to disagree, but I'm fairly sure about this. See the following links if you don't believe me: http://stackoverflow.com/questions/1506201/can-a-asp-net-mvc-view-page-have-a-webform-control-on-it , http://weblogs.asp.net/rajbk/archive/2010/05/11/running-asp-net-webforms-and-asp-net-mvc-side-by-side.aspx , http://www.hanselman.com/blog/PlugInHybridsASPNETWebFormsAndASPMVCAndASPNETDynamicDataSideBySide.aspx , and MANY more. By the way, WHO said anything about using them inside razor files? OP was talking about aspx engine. – walther Aug 25 '12 at 09:31
  • but you need to put a form with runat="server" inside your ASP.NET MVC masterpage. Would you do this in an ASP.NET MVC application? I wouldn't :-) Not to mention that those controls will be attempting to invoke some server side events in your code behind. Do you have code behind in your MVC applications? – Darin Dimitrov Aug 25 '12 at 09:31
  • @DarinDimitrov, so? What's your point? Who said something else? Furthermore, I never said I recommend to use this, quite the contrary! – walther Aug 25 '12 at 09:32
  • My point is that you cannot use server side controls in ASP.NET MVC, as I've already stated :-) But it's a good thing that you discourage their usage. I guess it's one thing we both agree upon. – Darin Dimitrov Aug 25 '12 at 09:33
  • @DarinDimitrov, I've provided the links and you said basically the same, yet you continue to argue. Feel free to continue, but without me. – walther Aug 25 '12 at 09:34
  • No worries mate, the links you've provided show indeed that you could place a server side control in an WebForms view. But that's as far as you will be able to get with it. You won't be able to actually serve it properly. Things like model binding, validation, callbacks, ... – Darin Dimitrov Aug 25 '12 at 09:35
  • You know, there's a slight difference between expressions `YOU CAN'T` and `you shouldn't`, don't you agree? – walther Aug 25 '12 at 09:37
  • Yes, I absolutely agree with you that there's a difference. But in this case it's like buying a car and never put petrol in the engine. The purpose is close to none. But once again I agree with you. I probably shouldn't have said *can't*. It's just that the OP was asking about actually developing MVC applications with server side controls, not just having fun experimenting with things. But I might be wrong of course. – Darin Dimitrov Aug 25 '12 at 09:39
0

I hate those razor helpers. (LabelFor, TextboxFor...)

ASP.NET MVC is a very different pattern and concept than classic WebForms. If you don't like those new concepts this probably means that ASP.NET MVC is not for you. You could always go back to the classic WebForms development.

In brief how do I write HTML w/o using helpers ?

There's nothing in ASP.NET MVC that forces you to use helpers. You could perfectly fine write pure static HTML in your views:

<form action="/home/save" action="post">
    <label for="first_name">First name</label>
    <input type="text" name="first_name" id="first_name" />

    <label for="item">Select an item</label>
    <select id="item" name="item">
        <option value="1">item 1</option>
        <option value="2">item 2</option>
        <option value="3">item 3</option>
    </select>

    <button type="sybmit">OK</button>
</form>

Obviously now you can forget about things like automatic data binding from the model, validation, routing, ...

As far as the problem you have with the ListBox inside a form with runat="server" is concerned, it seems that you have used some classic WebForms server side control in an ASP.NET MVC application which is not supported. Server side controls don't work in MVC because they depend on things like ViewState and require you to place them inside forms with runat="server", ... things that no longer exist in MVC.

So I would recommend you to go ahead and read some getting started tutorials about MVC here: http://asp.net/mvc

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Yes but you mention only those basic html elements. what about listbox dropdownbox etc ? and about your first answer. Yes I want to work in MVC but only if they made things little bit easier.. – Jalle Aug 25 '12 at 09:22
  • What about them? They are there and work great. For example you have the `Html.DropDownListFor` and the `Html.ListBoxFor` helpers that allow you to achieve that in MVC. – Darin Dimitrov Aug 25 '12 at 09:23
  • I am talking about pure html syntax. So to rephrase my question . are razor helkpers only way to write code or there is pure HTLP also ? – Jalle Aug 25 '12 at 09:24
  • No, of course that helpers are not the only way to achieve that. As I already explained and showed in my answer you could write pure static HTML. But please never do this because you will loose much of the benefits and functionality provided by the helpers and you will hate MVC even more :-) – Darin Dimitrov Aug 25 '12 at 09:24
  • So I see all experts are here but no one to answer 1 simple question: **How do you write Listbox control WITHOUT using Razor @HTML.listBox helper ?** – Jalle Aug 25 '12 at 09:51
  • Html.ListBox is not Razor specific. You could use it with the WebForms view engine: `<%= Html.ListBoxFor(...) %>`. If you don't want to use helpers and hardcode all your HTML then simply use a ` – Darin Dimitrov Aug 25 '12 at 22:26
0

A ListBox is a .Net-ified SELECT element. In a razor template you can probably do something like:

<select id="list1">
  @for // some loop condition
  {
    <option value="@Data.someValue">Text1</option>
  }
</select>

... where @Data is bound to a model, and Text1 could also be similarly declared.