我有一个使用未在提交表单的回发更新文本框的值一个模型视图对象的形式。 在提交表单,我编辑被绑定到一个文本框对象的属性。 当窗体回来,对象属性仍然改变,但文本框的值不会改变。 这就像文本框的值是缓存的,不会改变。 我该如何解决?
文本框的默认值:“”
文本框代码:
@Html.TextBoxFor(m => m.Project.tNumber, new { @readonly = "readonly", @class = "readonly" })
对象属性:
[Display(Name = "T Number")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public string tNumber { get; set; }
控制器的操作方法:
[Authorize(Roles = "Admin, OrderEntryManager")]
[HttpPost]
public ActionResult Verify(string submit, OrderEntryEdit model)
{
MembershipUser user = Membership.GetUser(User.Identity.Name);
int userId = WebSecurity.GetUserId(User.Identity.Name);
if (userId > 0)
{
if (ModelState.IsValid)
{
string ButtCommand = submit;
switch (ButtCommand)
{
case "Create Order":
if (model.CreateOrder(userId))
{
ViewBag.Status = "success";
ViewBag.Message = "The order has been created.";
}
else
{
ViewBag.Status = "error";
ViewBag.Message = "There was a problem in trying to create this order.";
}
default:
ViewBag.Status = "error";
ViewBag.Message = "Unrecognized form action.";
}
}
}
else
{
ViewBag.Status = "error";
ViewBag.Message = "Unrecognized user.";
}
return View("Verify", model);
}
视图模型方法:
public class OrderEntryEdit : OrderEntry
{
public OrderEntryEdit()
{
base.Project = new Project();
base.ShipTo = new ShipTo();
base.SoldTo = new SoldTo();
base.Unit = new List<Unit>();
}
//method simplified, but is reaching this method
public Boolean CreateOrder(int adminUserId = 0)
{
this.Project.tNumber = "T123456";
return true;
}
}
文本框的值:“”
编辑:更换代码这个文本框:
<input type="text" readonly="readonly" class="readonly" value="@Model.Project.tNumber" />
有固定的问题。 显然,文本框被缓存。 总之,不要使用剃刀语法,良好的旧的HTML工作正常。 我希望这可以帮助别人!