Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
@Html.TextBoxFor(m=> s.MyName, new { htmlAttributes = new { @class = "form-control" } })


MyName property has a value "TestUser" in db.
When i use the above line it displays a textbox but it is not empty, it has a value "TestUser". How to display an empty textbox? (considering an ex of a registration form) in mvc 5
Posted
Updated 2-Jul-15 19:54pm
v3
Comments
Sreekanth Mothukuru 3-Jul-15 3:09am    
Just assign null or empty string to the model property "MyName" before returning the view from the controller

1 solution

The model you send to the view is not empty and therefore it will not be empty. If you will make an registration form consider something like this.

C#
[HttpGet]
public ActionResult Register()
{
    return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        // Register user logic
        return RedirectToAction("Index", "Home");
    }

// If we got this far, something failed, redisplay form
return View(model);
}

HTML
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <h4>Create a new account.</h4>
    @Html.ValidationSummary()
    <div class="form-group">
        @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
        </div>
    </div>
    <!-- More inputs or something -->
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Register" />
        </div>
    </div>
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900