tags:

views:

50

answers:

3

Hi , 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.

A: 

Hi, Vahe. I have such a problem, but I think that we both don't understand MVC application lifecycle. Let wait other answers

vaa
A: 

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!

I think the OP has the issue when he posts to the action. A default value is only set, when there is no value for Title in the post.
Malcolm Frexner
just have a try on my sugesstion!BTW i don't understander what did you mean above.
+1  A: 

A similar question has only recently been asked: http://stackoverflow.com/questions/1786393/set-value-for-textbox-in-action/1786431#1786431

queen3
Yes thats kind of strange. I asked the question yesterday. (See here why it is not strange - Buses always come in threes http://www.maa.org/mathland/mathtrek_3_26_01.html). I did not accept the answer yet because I have to test it more, but it looks like the right solution.
Malcolm Frexner
The buses explanation does not work for SO questions ;-)
queen3