@ Html.TextBoxFor(m => m.Id)重新调整ID的错误值

在我的家庭控制器中,我有一个针对这样设置的编辑表单的操作方法:

public actionResult Edit(int id)
{
    Person p = DbPerson.GetPerson(id);

    return View(p);
}

视图:

@model Person

<h4 class="leftBG">Edit Form</h4>

id is : @Model.Id<br />
Name is : @Model.Name<br /><br />

@Html.LabelFor(m => m.Id,new { })
@Html.TextBoxFor(m => m.Id,new { })

@Html.LabelFor(m=>m.Name,new { })
@Html.TextBoxFor(m => m.Name,new { })

当我导航至:home/edit/1 我得到了id = 1的人,并且一切正常。

但是,如果我去home/edit/199 (数据库中没有id为199的人) DbPerson.GetPereson会返回一个ID = 0的人

但是在我看来,id的文本框显示为:199,我不确定为什么...

这是预期的行为吗?

为什么Model.Id是0,而文本框却显示值199

xiaomao315452522 回答:@ Html.TextBoxFor(m => m.Id)重新调整ID的错误值

尝试使用ModelState.Clear();在返回视图之前。它可能会挂在旧值上。

本文链接:https://www.f2er.com/3149636.html

大家都在问