【发布时间】:2012-07-24 20:39:45
【问题描述】:
我正在尝试下面解释的基本 asp .net mvc 项目配置;
在第一次提交之前,结果在照片中是预期的,在提交文本框(@Html.TextBoxFor(model => model.Name))和文本(@Model.Name)后显示不同的值,如照片中的右侧所示,出乎意料;为什么会这样?尽管它们的模型是独一无二的,但它们显示出不同的值。
型号:
public class Personnel
{
public string Name { get; set; }
}
查看:
@model deneme.Models.Personnel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm("Index", "Home"))
{
@Html.TextBoxFor(model => model.Name)
<br/>
<br/>
@Model.Name
<br/>
<br/>
<input type="submit" value="submit" />
}
控制器:
public ActionResult Index(Personnel personnel)
{
if (string.IsNullOrEmpty(personnel.Name))
{
personnel.Name = "Ahmet";
}
else
{
personnel.Name = personnel.Name + "server";
}
return View(personnel);
}
【问题讨论】:
-
您第一次提交表单时是否未填写文本框。然后它根据您的代码工作正常
-
不,会出现同样的结果。它们仍然不同。
-
你用的是什么浏览器?我推测浏览器可能会自动填充表单域......
-
我在多个浏览器中尝试过,@Vedran 解决了这个问题。 ModelState.Clear() 解决。
标签: asp.net-mvc