gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:22:38 24 4
gpt4 key购买 nike

出现的内容如下:

White spaces?

我使用了模板,并将模型传递到我的 View 中,我注意到它们得到了那些空白。我一直试图为我的性别默认一个 DropDownList 但不知何故,它们不起作用。我试图调试并认为可能是因为那些奇怪的空格。 (除部门外,其他都有)。

如何解决这个问题?

我还想补充一点,我使用 Entity Framework

型号

[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);

}

查看

@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>

最佳答案

这是一个数据库问题。我敢打赌您设计的表的姓氏属性为 char 或 nchar。 Source .

如何解决?

  • 您可以编辑表格的设计并将类型替换为 varchar 或 nvarchar 或
  • 您可以在 C# 中使用 string.Trim() 函数来删除空格,但您必须在每个相关操作中执行此操作。

关于c# - ASP MVC : Weird white spaces on the textbox whenever i try to edit a record,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32190805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com