• Now Online : 14
  • admin@codemyne.net

Difference between @Html.TextBox and @Html.TextBoxFor: @Html.TextBox is not strongly typed that means its name and value can be hardcoded where as @Html.TextBoxFor requires a strongly typed view and uses the view model. The helper will use the lambda expression to infer the name and the value of the view model passed to the view. And because it is a good practice to use strongly typed views and view models you should always use the Html.TextBoxFor helper.

In this example 'LoginViewModel' is used to bind the helpers like Label, TextBox, Password and CheckBox as shown below.You can see this model class in Models folder.

@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
        @Html.AntiForgeryToken()
        <h4>Use a local account to log in.</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
                @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
                            </div>
                            </div>
                            <div class="form-group">
                @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
                            </div>
                            </div>
                            <div class="form-group">
                            <div class="col-md-offset-2 col-md-10">
                            <div class="checkbox">
                        @Html.CheckBoxFor(m => m.RememberMe)
                        @Html.LabelFor(m => m.RememberMe)
                            </div>
                            </div>
                            </div>
                            <div class="form-group">
                            <div class="col-md-offset-2 col-md-10">
                            <input type="submit" value="Log in" class="btn btn-default" />
                            </div>
                            </div>
                            <p>
        @Html.ActionLink("Register as a new user", "Register")
        </p>
               
 }

The model used for login is as shown below

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment