I have a form that uses a modelview object that is not updating a textbox value on postback of submitting a form. On submitting a form, I edit the property of an object that is binded to a textbox. When the form comes back, the object property is still changed, but the textbox value does not change. It's like the textbox value is cached and will not change. How do I fix this?
Textbox default value: ""
Textbox code:
@Html.TextBoxFor(m => m.Project.tNumber, new { @readonly = "readonly", @class = "readonly" })
Object property:
[Display(Name = "T Number")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public string tNumber { get; set; }
Controller Action Method:
[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);
}
ViewModel method:
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;
}
}
Textbox value: ""
Edit: replacing the code for textbox with this:
<input type="text" readonly="readonly" class="readonly" value="@Model.Project.tNumber" />
has fixed the problem. Apparently, the textbox is being cached. In short, don't use razor syntax, good old html works fine. I hope this helps someone else!