asp.net request.form

本小妞迷上赌 提交于 2019-12-07 15:48:33

问题


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?


回答1:


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"



来源:https://stackoverflow.com/questions/11050677/asp-net-request-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!