Reputation: 1879
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
Upvotes: 0
Views: 255
Reputation: 621
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.
Upvotes: 1
Reputation: 26096
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.
Upvotes: 0