views:

50

answers:

2

HI, Does anyone know how can I receive the modified model value which I have bound to controls on view page. For instance I have used a text box on view, as follows-

<%=Html.TextBoxFor(model => Model.firstName, new { id = "txtFirstName"})%>

But in my post method I am not able to receive the modified value from the text box. Any solution?

Thanks, Kapil

A: 

Have you tried DummyAction without the FormCollection parameter? I.e.:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DummyAction(clsModel modelObject)
{
     // ...
}

You most likely shouldn't need it anyway if you are using clsModel in your view.

mgroves
+1  A: 

You have renamed your model's required field to txt....

The framework maps html fields by name to your class properties as there is no txtFirstName in your class it cannot automatically map them. (as the html field name and the model property name must match for binding)

if you do the following it will bind correctly

<%=Html.TextBoxFor(model => Model.FirstName)%>

[HttpPost]
public ActionResult Edit(ClsUser myObject)
{
    var x = myObject.FirstName; // this will now have a value
}

If you want to use the renamed field i.e. { id = "txtFirstName"} then you can either create a new class and bind to that as in:

public class ClsUserReturn
{ 
    public string txtFirstName{get;set;}
}

and in your controller

[HttpPost]
public ActionResult Edit(ClsUserReturn myObject)
{
    var x = myObject.txtFirstName; // this will now have a value
}

or

You could go further and define a custom binder, to strip the txt or other prefix/s if your naming standards dictate that you have to use specific prefixes txt/cbo/chk etc.

AJ
+1, This was the first thing that jumped out at me.
Alastair Pitts
The name of the property should be firstName. It isn't the id that is posted to the server, it is the name. The name isn't changed when he sets the id to something else.
Mattias Jakobsson