Carlos Miguel Colanta
Carlos Miguel Colanta

Reputation: 2823

ASP MVC: Weird white spaces on the textbox whenever i try to edit a record

here is what appears:

White spaces?

I used the template and i passed my model onto my view and i notice that they get those white spaces. I've been trying to default a DropDownList for my gender but somehow, they don't work. I was trying to debug and thought that maybe it's because of those weird white spaces. (All of them have it except department).

How do i resolve the issue?

I would also like to add that i use Entity Framework.

Model

[Table("carl")]
public class Employee
{


    public int id { get; set; }

    [DisplayName("First Name")]
    [Required(ErrorMessage = "First name is required")]
    public string firstname { get; set; }


    [DisplayName("Last name")]
    [Required(ErrorMessage = "Last name is required")]
    public string lastname { get; set; }

    [Required(ErrorMessage = "Gender is required")]
    public string gender { get; set; }


    [DisplayName("Department")]
    [Range(1, 3)]
    [Required(ErrorMessage = "Dep name is required")]
    public int department { get; set; }

    public List<String> Players = new List<String> { "test", "test" };
}

Controller

 [HttpGet]
    public ActionResult Edit(int id)
    {

        EmployeeContext emp = new EmployeeContext();
        Employee sel = emp.Employees.Single(x => x.id == id);

        return View(sel);

    }

View

@model MvcApplication6.Models.Employee

@{
ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Employee</legend>

    @Html.HiddenFor(model => model.id)

    <div class="editor-label">
        @Html.LabelFor(model => model.firstname)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.firstname)
        @Html.ValidationMessageFor(model => model.firstname)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.lastname)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.lastname)
        @Html.ValidationMessageFor(model => model.lastname)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.gender)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.gender)
        @Html.ValidationMessageFor(model => model.gender)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.department)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.department)
        @Html.ValidationMessageFor(model => model.department)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

Upvotes: 3

Views: 3528

Answers (2)

Sai Gummaluri
Sai Gummaluri

Reputation: 1394

It has been said that all the fields except the department have spaces. Since all other fields except department are of string type, ideally you should be modifying the columns' datatypes in the DB by a type like nvarchar in SQL or varchar2() in Oracle.

As others have mentioned, you can also handle it in the code-behind as well using the Trim().

Further, scaffolding always gives you robust support for performing any CRUD operations. Please go through this post. This outlines how scaffolding works out with Entity Framework.

Cheers!

Upvotes: 1

Giu
Giu

Reputation: 1952

It's a database problem. I would bet you designed your table with a lastname property as char or nchar. Source.

How to solve it ?

  • You can edit the design of your table and replace the type by varchar or nvarchar or
  • You can use string.Trim() function in c# to delete white spaces but you will have to do that in every action related.

Upvotes: 5

Related Questions