Reputation: 11
I have such a problem. I change my models fields in controller but doesn't see the changes.
Here are the parts of code :
this is view Index.aspx
<%Html.BeginForm("Index", "First");%>
<p>
<%=Html.TextBox("Title") %>
</p>
<p>
<%=Html.TextBox("Price") %>
</p>
<input type="submit" value="Submit" />
<%Html.EndForm();%>
this is controller:
FirstController.cs
public class FirstController : Controller
{
[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Index()
{
return View(new MyModel());
}
[AcceptVerbs(HttpVerbs.Post)]
public ViewResult Index(MyModel k)
{
k.Title = "Title";
return View(k);
}
and this is model:
MyModel.cs
public class MyModel {
public String Title { get; set; }
public Decimal Price { get; set; }
}
when I change "Title" text box in controller I don't see changes in view
public ViewResult Index(MyModel k)
{
k.Title = "Title";
return View(k);
}
text box keep its value before submit. Is there any mistake in my code.
This problem doesn't appear when I use html standart input tag instead of Html.TextBox:
input type="text" id="Title" name="Title" value="<%=Model.Title %>
Thank you in advance.
Upvotes: 1
Views: 909
Reputation: 21
public ViewResult Index(MyModel k)
{
ModelState.Clear();
k.Title = "Title";
return View(k);
}
Have you tried to add ModelState.Clear() as in the above sample? Hope this helps!
Upvotes: 2
Reputation: 41
you should do it like this:
<%Html.BeginForm("Index", "First");%>
<p>
<%=Html.TextBox("Title",Model.Title) %>
</p>
<p>
<%=Html.TextBox("Price",Model.Price) %>
</p>
<input type="submit" value="Submit" />
<%Html.EndForm();%>
The Html.TextBox() have a overwrite version take object value arg to populate the text value!
Upvotes: 0
Reputation: 1
I have such a problem, but I think that we both don't understand MVC application lifecycle. Let wait other answers
Upvotes: 0