asp.net-mvcservercontrol

What is the syntax to embed a server control in an ASP text box? I can't find a working example anywhere


I'm new to ASP MVC and I'm completely stuck.

All I want to do is have a control like this:

<td><asp:TextBox ID="txbFirstName" runat="server" Value=""></asp:TextBox></td>

But have it populated by a value from a controller.

This syntax is what I was expecting would work:

<td><asp:TextBox ID="txbFirstName" runat="server" Value="<% Model.FirstName%>"></asp:TextBox></td> 

But obviously it doesn't. I hope from that you can tell what I'm trying to do (populate the value of the text box with a server control) but that won't work. Is this not possible using ASP:Textboxes? Do I need to use HTML boxes instead?

Apparently in ASP 4 MVC the <% %> tags are no longer used and I cannot find a working example anywhere. I thought this would be a simple google but I've been stuck for hours.

Thank you for any help you can provide.


Solution

  • Microsoft MVC doesn't use server side controls. You should use HTML Helpers and Model Bindings with Razor view engine (better than ASP.NET view engine).

    In the view:

    @Html.EditorFor(model => model.Firstname)

    A simple Login form:

            @using (Html.BeginForm("LoginAction", "LoginController"))
            {
    
                @Html.LabelFor(model => model.User, "Your UserName")
                @Html.EditorFor(model => model.User)
    
                @Html.LabelFor(model => model.Password, "Your Password:")
                @Html.PasswordFor(model => model.Password)
    
                <button type="submit">Submit</button>
            }
    

    See http://www.asp.net/mvc/overview/getting-started