3
public ActionResult DisplayCustomer()
{
    Customer objCustomer = new Customer();

    objCustomer.Id = Convert.ToInt16(Request.Form["Customerid"]);
    objCustomer.CustomerCode = Request.Form["code"];
    objCustomer.Amount = Convert.ToDouble(Request.Form["amount"]);
    return View(objCustomer);
}

This is my action in controller(MVC 2 aspx):

<form action="DisplayCustomer" method="post">
    Customer id:-
    <input type="text" id="Customerid" /><br />
    Customer Code:-
    <input type="text" id="code" /><br />
    Customer amount:-
    <input type="text" id="amount" /><br />
    <input type="submit" value="click here" />
</form>

This is the view. When someone hit the submit button it is directed to a new page:

<div>
   <b> ID</b> <%= Model.Id %><br />
   <b> Code </b><%=Model.CustomerCode %><br />
   <b> Amount</b> <%=Model.Amount %><br />
</div>

I always get 0 in Id and Amount while blank in CustomerCode.

Any clue? Why this is happening? What is wrong?

betabandido
  • 16,863
  • 10
  • 53
  • 69
ghost...
  • 893
  • 1
  • 14
  • 32
  • 4
    You're misusing ASP.Net MVC. You need to use model binding and HTML helpers. – SLaks Jun 15 '12 at 12:34
  • Yes use model binding your action methods would not be testable if you pull data from request form. MVC is designed keeping in mind TDD. Use`name=`"Customerid`"` as suggested by Aristos. IF you use model to render the view, view engine will take care of this by default. – Abhijit-K Jun 15 '12 at 12:42

1 Answers1

5

Your issue here is that you set the id, and not the name. Set the name to get the post back value. Eg:

<input type="text" name="Customerid" />

read also: HTML input - name vs. id in the line "Used on form elements to submit information"

Community
  • 1
  • 1
Aristos
  • 63,580
  • 14
  • 112
  • 146
  • @shaleen You are welcome. I do not know if you have made a design error here as Slaks say, because I am not work with mvc. I only know that this is the reason that you not get the form values as I see your code. – Aristos Jun 15 '12 at 12:44